Note that there are some explanatory texts on larger screens.

plurals
  1. POSql Server Replace function inside of IN operator
    primarykey
    data
    text
    <p>I am trying to build a dynamic sql query.</p> <p>I have a field which is a string of pipe delimited Guids.</p> <p>I want to execute a query that finds all Users with the Guids in this field.</p> <p>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.</p> <pre><code>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') </code></pre> <p>UPDATE: Here are some SqlFiddle Queries to further show what I am doing.</p> <p>Select all shows 2 records <a href="http://sqlfiddle.com/#!3/a5e426/5" rel="nofollow">http://sqlfiddle.com/#!3/a5e426/5</a></p> <p>Select with hardcoded subquery <a href="http://sqlfiddle.com/#!3/a5e426/6" rel="nofollow">http://sqlfiddle.com/#!3/a5e426/6</a></p> <p>Select with pipe delimited Guids trying to be replaced for the IN subquery <a href="http://sqlfiddle.com/#!3/a5e426/7" rel="nofollow">http://sqlfiddle.com/#!3/a5e426/7</a></p> <p>Subquery select to show I am getting what I want <a href="http://sqlfiddle.com/#!3/a5e426/8" rel="nofollow">http://sqlfiddle.com/#!3/a5e426/8</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload