I am trying to build a dynamic sql query.
I have a field which is a string of pipe delimited Guids.
I want to execute a query that finds all Users with the Guids in this field.
In the example below I have just added a couple of dummy GUIDs for examples. My second query works as I want it to. My first query with the REPLACE function is not working. If I execute just the Replace Select it does return what I have in the second query so I would expect it to return the same results. Can someone please help me out with what I am missing here? Thanks.
select * from CMS_User
WHERE Convert(varchar(36),UserGUID) IN(
select '''' + REPLACE('6415B8CE-8072-4BCD-8E48-9D7178B826B7|AEDD6E3F-61C1-46CF-B2D4-750180036FFF','|',''',''') + ''''
)
select *
from CMS_User
WHERE Convert(varchar(36),UserGUID) IN('6415B8CE-8072-4BCD-8E48-9D7178B826B7','AEDD6E3F-61C1-46CF-B2D4-750180036FFF')
UPDATE: Here are some SqlFiddle Queries to further show what I am doing.
Select all shows 2 records http://sqlfiddle.com/#!3/a5e426/5
Select with hardcoded subquery http://sqlfiddle.com/#!3/a5e426/6
Select with pipe delimited Guids trying to be replaced for the IN subquery http://sqlfiddle.com/#!3/a5e426/7
Subquery select to show I am getting what I want http://sqlfiddle.com/#!3/a5e426/8