sql server - How to Pivot one Row into One Column -


can please me out. i've looked around , can't find similar need do. basically,

i have table need pivoted, coming flat file loads columns 1 comma delimited column. need break out columns respective order before pivot , i've got procedures beautifully. however, crux of table need edit headers before can continue.

i need pivot information in first column , put table i've created. therefore, need this

    id   column01      1    express,,,express,,,hypermakert,,webstore,web 

to end this....

    new_id   new_col      1        express     2             3     4        express     5     6             7        hypermarket     8     9        webstore     10       web 

please note need include '' black columns original row and.
looked , links below not helpful;

sql server : transpose rows columns efficiently convert rows columns in sql server mysql query dynamically convert rows columns

there many methods of splitting string in sql server can find on web, complicated simple. way of using dynamic query. it's short , simple (not sure performance believe not bad):

declare @s varchar(max) -- save column01 string/text @s variable select @s = column01 test id = 1 -- build query string set @s = 'select row_number() on (order current_timestamp) new_id, c new_col (values ('''           + replace(@s, ',', '''),(''') + ''')) v(c)'  insert newtable exec(@s)     go select * newtable 

sqlfiddle demo

the use of values() clause above kind of anonymous table, here simple example of such usage (so can understand better). anonymous table in following example has 1 column, table name v , column name c. each row has 1 cell , should wrapped in pair of parentheses (). rows separated commas , follow after values. here code:

-- note outside (...) wrapping values .... select * (values ('a'),('b'),('c'), ('d')) v(c) 

the result be:

   c ------ 1  2  b 3  c 4  d 

just try running code , you'll understand how useful is.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -