Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert rows into columns. here is my sample table.

tbl_table

FId Type
6 Number
8 Name
15 Street
18 Address

Here is the query I have worked out.

SELECT SUBSTRING((SELECT ',' + CAST(FId AS VARCHAR) FROM tbl_table where Type in('Address','Street','Number','Name')FOR XML PATH('')), 2,10000) AS FID

I received output as

FId
6,8,15,18

It has been converted rows into columns, But my problem is,

I need the output in same order which input i have passed inside.

My expected output should be as

FId
18,15,6,8


Kindly resolve this problem.

What I have tried:

SELECT SUBSTRING((SELECT ',' + CAST(FId AS VARCHAR) FROM tbl_table where Type in('Address','Street','Number','Name')FOR XML PATH('')), 2,10000) AS FID
Posted
Updated 5-May-17 8:10am
Comments
CHill60 5-May-17 7:03am    
If I enter data into the table in the order you mention then I get your expected results "18,15,6,8".
If you are concerned in maintaining the order the records are entered into the table then you will need to record that in some way - either an IDENTITY column or a SEQUENCE (SQL 2012 and later)

You need to specify the desired order within the sub-query.

Try something like this:
SQL
SELECT STUFF(
    (SELECT ',' + CAST(FId As varchar(20)) 
    FROM tbl_table 
    WHERE [Type] IN ('Address', 'Street', 'Number', 'Name')
    ORDER BY CASE [Type] 
        WHEN 'Address' THEN 0 
        WHEN 'Street' THEN 1 
        WHEN 'Number' THEN 2 
        ELSE 3 
    END
    FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
), 1, 1, '') AS FID

The STUFF function[^] avoids having to provide an end point for the substring operation.

And adding TYPE and .value('.', 'varchar(max)') ensures there won't be any XML-encoded text in the output, as described in this Simple Talk article[^].
 
Share this answer
 
Declare @products varchar(200)
set @products= 'Address,Street,Number,Name'
Declare @individual varchar(20)
Declare @count varchar(200)
set @count = ''
WHILE LEN(@products) > 0
BEGIN
    IF PATINDEX('%,%', @products) > 0
    BEGIN
        SET @individual = SUBSTRING(@products,
                                    0,
                                    PATINDEX('%,%', @products))
         
        SET @products = SUBSTRING(@products,
                                  LEN(@individual + ',') + 1,
                                  LEN(@products))
		select @count = @count + ' ' + convert(varchar,fid) from tbl_table where [type] =@individual
		
    END
    ELSE
    BEGIN
        SET @individual = @products
        SET @products = NULL
        select @count = @count + ' ' + convert(varchar,fid) from tbl_table where [type] =@individual
        select @count
    END
END
 
Share this answer
 
v2
Comments
vinodh muthusamy 5-May-17 7:03am    
Parameter which i have given is sample, But it will be more than 20 or 30. All those are dynamcic.
CHill60 5-May-17 7:04am    
This returns "47" - not quite what the OP wanted. It is also a truly awful query.
vinodh muthusamy 5-May-17 7:08am    
This query adds all value and execute the query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900