sql server - Quoted and Unquoted comma separated list in T-SQL -
i've requirement need select column in sql table 2 separate fields containing comma separated values - 1 should quoted , other 1 should unquoted.
so example, if table has values b c d e in single column, select sql statement should return 2 fields - 1 field containing values 'a','b','c','d','e' , other 1 containing values a,b,c,d,e.
i'm running following sql statement achieve this:
select substring((select top 100 ''',''' + cast(sales_id nvarchar(50)) sales_order_header (status = 0) order updated_datetime, 1 xml path('')),3,2147483647) + '''' quoted, substring((select top 100 ',' + cast(sales_id nvarchar(50)) sales_order_header (status = 0) order updated_datetime, 1 xml path('')),2,2147483647) + '' unquoted
however, had scenario results "quoted" , "unquoted" column different though running same "select" statement in substring function.
what best way achieve want ?
is there way write sql statement such queries table once , reuses results both columns rather running "select" statement twice.
you can avoid repeated duplication of query building basic query cte or derived table. assuming sales_id
numeric, building unquoted version quoted version can achieved replacing out quotes:
with mycte(col) ( select top 100 ''',''' + cast(sales_id nvarchar(50)) sales_order_header (status = 0) order updated_datetime, 1 xml path('') ) select substring(col, 3,2147483647) + '''' quoted, substring(replace(col, '''', ''),2,2147483647) + '' unquoted mycte;
Comments
Post a Comment