I am trying to use a carriage return separated list of parameters in an IN
list of the where
statement of my query.
I can turn the list into one comma separated string in the correct format using replace
function, however when I put this in the IN
list, it returns nothing.
The query below returns the comma separated list as expected.
declare @VarCodes varchar(max)
set @VarCodes = '123-1
123-10
123-100
61
66
67
75'
(select ''''+replace(replace(REPLACE(@VarCodes,char(13),''''+', '+''''),char(32),''),char(10),'')+'''')
'123-1','123-10','123-100','61','66','67','75'
If I paste this text directly in the query below, it returns data as expected.
select vad_variant_code from variant_detail where vad_variant_code in ('123-1','123-10','123-100','61','66','67','75')
If I put the parameter in the in
, it returns nothing.
select vad_variant_code from variant_detail where vad_variant_code in ((select ''''+replace(replace(REPLACE(@VarCodes,char(13),''''+', '+''''),char(32),''),char(10),'')+''''))
I am assuming this is because the IN
is expecting a comma separated list of strings, where as the replace
function is returning one long string?
Can this be achieved?