Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 420055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:50:18+00:00 2026-05-12T18:50:18+00:00

my goal is if I have this: colmuns c1 | c2 | c3 |

  • 0

my goal is if I have this:

colmuns      c1 | c2 | c3 | c4 | c5 | n..
row1          a |  a |  a |  a |  a | 
row2          b |  b |  b |  b |  b |
rowN...

I want to do a query that would return

   myCol
   aaaaa
   bbbbb
   nnnnn...

I know I can do this

select t2.id, (
    select  *
    from mytable t1
    where t1.id= t2.id
    for xml path('')) as row
from mytable t2

and it will put the whole row with many columns into one column like I want

now, how to filter out the xml tag?

or is there any other solution?

edit
column might be null are not varchar, could be int, varchar, date, etc

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:50:19+00:00Added an answer on May 12, 2026 at 6:50 pm

    try:

    ;with XmlValues  as
    (
        select t2.id, (
            select  *
            from mytable  t1
            where t1.id= t2.id
            for xml path(''), TYPE) as row
        from mytable  t2
    )
    select x.row.value('.', 'VARCHAR(8000)') as readable
        FROM XmlValues AS x
    

    EDIT working sample:

    DECLARE @YourTable table (c1 int, c2 int, c3 varchar(5), c4 datetime)
    INSERT INTO @YourTable VALUES (1,2,'abcde','1/1/2009')
    INSERT INTO @YourTable VALUES (100,200,'zzz','12/31/2009 23:59:59')
    
        select t2.c1, (
            select  *
            from @YourTable  t1
            where t1.c1= t2.c1
            for xml path(''), TYPE) as row
        from @YourTable  t2
    
    ;with XmlValues  as
    (
        select t2.c1, (
            select  *
            from @YourTable  t1
            where t1.c1= t2.c1
            for xml path(''), TYPE) as row
        from @YourTable  t2
    )
    select x.c1,x.row.value('.', 'VARCHAR(8000)') as readable
        FROM XmlValues AS x
    

    OUTPUT:

    c1          row
    ----------- --------------------------------------------------------------------
    1           <c1>1</c1><c2>2</c2><c3>abcde</c3><c4>2009-01-01T00:00:00</c4>
    100         <c1>100</c1><c2>200</c2><c3>zzz</c3><c4>2009-12-31T23:59:59</c4>
    
    (2 row(s) affected)
    
    c1          readable
    ----------- ----------------------------------
    1           12abcde2009-01-01T00:00:00
    100         100200zzz2009-12-31T23:59:59
    
    (2 row(s) affected)
    

    EDIT loop free way to parse table column names from meta data tables, with the ability to format each datatype as desired and supports NULLs:

    BEGIN TRY 
    CREATE TABLE YourTable (c1 int, c2 int, c3 varchar(5), c4 datetime) 
    INSERT INTO YourTable VALUES (1,2,'abcde','1/1/2009')
    INSERT INTO YourTable VALUES (100,200,'zzz','12/31/2009 23:59:59')
    end try begin catch end catch
    
    DECLARE @YourTableName   varchar(1000)
    DECLARE @YourColumns     varchar(max)
    DECLARE @YourQuery       varchar(max)
    SET @YourTableName='YourTable'
    SELECT
        @YourColumns=STUFF(
                           (SELECT
                                '+ '
                                --' ' --any constant string to appear between columns
                                + CASE DATA_TYPE
                                      WHEN 'datetime' THEN 'COALESCE(CONVERT(char(23),'+CONVERT(varchar(max),COLUMN_NAME)+',121),''NULL'')'
                                      --more datatypes here
                                      ELSE 'COALESCE(CONVERT(varchar(max),' + CONVERT(varchar(max),COLUMN_NAME)+'),''NULL'')'
                                  END
                                FROM INFORMATION_SCHEMA.COLUMNS
                                WHERE table_name = @YourTableName
                                FOR XML PATH('')
                           ), 1, 2, ''
                          )
    
    SET @YourQuery  = 'SELECT '+@YourColumns+' FROM '+@YourTableName
    PRINT @YourQuery  
    SELECT * FROM YourTable
    
    EXEC (@YourQuery)
    

    OUTPUT:

    SELECT COALESCE(CONVERT(varchar(max),c1),'NULL')+ COALESCE(CONVERT(varchar(max),c2),'NULL')+ COALESCE(CONVERT(varchar(max),c3),'NULL')+ COALESCE(CONVERT(char(23),c4,121),'NULL') FROM YourTable
    c1          c2          c3    c4
    ----------- ----------- ----- -----------------------
    1           2           abcde 2009-01-01 00:00:00.000
    100         200         zzz   2009-12-31 23:59:59.000
    
    (2 row(s) affected)
    
    
    ------------------------------------------
    12abcde2009-01-01 00:00:00.000
    100200zzz2009-12-31 23:59:59.000
    
    (2 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal of this SQL query is to return the user with the most
I have this function. And its goal is to take the last character first
I have data like this: Team | goal a | 5 b | 8
My goal is to have the url routing as following: http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page What is
Here's my ultimate goal in all of this. I have a viewcontroller with a
goal: I have the string 1234432144 I want to only replace the first 2
I have a table that has several columns of textual data. The goal is
My goal is to get a query written. I have three tables, A, B
So I have this basic html form. My goal is to pass the values
This is for MySQL and PHP I have a table that contains the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.