Pages

Wednesday, October 14, 2015

Sql things -- create comma separated list of values of a column in ms sql

1. To convert integer column values to varchar in ms sql


1. SELECT CONVERT(varchar(10), PType_ID) FROM PType

2. create comma separated list of values of a column in ms sql 

Declare @Str varchar(max)
select @Str = coalesce (@Str +',' ,'') + convert(varchar(10) , PriceType_ID )
from PriceType with (NOLOCK)
where PriceTypeCode in ('01', '02', '03', '04')

select @lStr as val

note: here PriceType_ID  is a integer column so i need to convert it to varchar.
in case if this column is already varchar then you do not need to use convert function.