How can i find if a string is present in a collection of comma seprated strings in sql server?











up vote
-1
down vote

favorite












I want to find if a string is present among a collection of string in sql .
Given code is always returning false.Can anyone plz tell me where am i wrong ?



alter procedure is_exception_user @usrid varchar(100)
as
begin
if @usrid in ('abc,xyz')
SELECT 'TRUE'
ELSE
SELECT 'FALSE'
END

exec is_exception_user 'xyz'









share|improve this question






















  • What version of SQL Server?
    – Aaron Bertrand
    Nov 10 at 14:54















up vote
-1
down vote

favorite












I want to find if a string is present among a collection of string in sql .
Given code is always returning false.Can anyone plz tell me where am i wrong ?



alter procedure is_exception_user @usrid varchar(100)
as
begin
if @usrid in ('abc,xyz')
SELECT 'TRUE'
ELSE
SELECT 'FALSE'
END

exec is_exception_user 'xyz'









share|improve this question






















  • What version of SQL Server?
    – Aaron Bertrand
    Nov 10 at 14:54













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to find if a string is present among a collection of string in sql .
Given code is always returning false.Can anyone plz tell me where am i wrong ?



alter procedure is_exception_user @usrid varchar(100)
as
begin
if @usrid in ('abc,xyz')
SELECT 'TRUE'
ELSE
SELECT 'FALSE'
END

exec is_exception_user 'xyz'









share|improve this question













I want to find if a string is present among a collection of string in sql .
Given code is always returning false.Can anyone plz tell me where am i wrong ?



alter procedure is_exception_user @usrid varchar(100)
as
begin
if @usrid in ('abc,xyz')
SELECT 'TRUE'
ELSE
SELECT 'FALSE'
END

exec is_exception_user 'xyz'






sql sql-server






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 14:40









Rehan Haque

206




206












  • What version of SQL Server?
    – Aaron Bertrand
    Nov 10 at 14:54


















  • What version of SQL Server?
    – Aaron Bertrand
    Nov 10 at 14:54
















What version of SQL Server?
– Aaron Bertrand
Nov 10 at 14:54




What version of SQL Server?
– Aaron Bertrand
Nov 10 at 14:54












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Is this the logic you want?



if @usrid in ('abc', 'xyz')
select 'TRUE'
else
select 'FALSE';


Or more simply:



select (case when @usrid in ('abc', 'xyz') then 'TRUE' else 'FALSE' END)





share|improve this answer




























    up vote
    0
    down vote













    You can do:



    IF ',' + 'abc,xyz' + ',' LIKE '%,' + @usrid + ',%'
    BEGIN
    SELECT 'TRUE';
    END


    The trick with the leading and trailing commas in both sides of the comparison serves two purposes: (1) to make sure '12' doesn't match '34,124' and (2) to catch when the one you're looking for is at the beginning and end of the string.



    And I wrote this this way:



    ',' + 'abc,xyz' + ','


    Because I assume you're not going to hard-code that big comma-separated string in your procedure and instead it's going to be derived from a column or a variable.



    In SQL Server 2016, you can instead do this:



    IF @usrid IN (SELECT Value FROM STRING_SPLIT('abc,xyz', ','))
    BEGIN
    SELECT 'TRUE';
    END


    Or just store your values in a table structure in the first place:



    DECLARE @values TABLE(Value varchar(32) PRIMARY KEY);
    INSERT @values VALUES('abc','xyz');

    IF @usrid IN (SELECT Value FROM @values)
    BEGIN
    SELECT 'TRUE';
    END


    Sets of individual values are much better in SQL Server than any kind of string or other structure that holds multiple values. Each piece of data should be its own fact. Combining them for convenience leads to pain at some point.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240019%2fhow-can-i-find-if-a-string-is-present-in-a-collection-of-comma-seprated-strings%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Is this the logic you want?



      if @usrid in ('abc', 'xyz')
      select 'TRUE'
      else
      select 'FALSE';


      Or more simply:



      select (case when @usrid in ('abc', 'xyz') then 'TRUE' else 'FALSE' END)





      share|improve this answer

























        up vote
        1
        down vote













        Is this the logic you want?



        if @usrid in ('abc', 'xyz')
        select 'TRUE'
        else
        select 'FALSE';


        Or more simply:



        select (case when @usrid in ('abc', 'xyz') then 'TRUE' else 'FALSE' END)





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Is this the logic you want?



          if @usrid in ('abc', 'xyz')
          select 'TRUE'
          else
          select 'FALSE';


          Or more simply:



          select (case when @usrid in ('abc', 'xyz') then 'TRUE' else 'FALSE' END)





          share|improve this answer












          Is this the logic you want?



          if @usrid in ('abc', 'xyz')
          select 'TRUE'
          else
          select 'FALSE';


          Or more simply:



          select (case when @usrid in ('abc', 'xyz') then 'TRUE' else 'FALSE' END)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 14:41









          Gordon Linoff

          742k32285390




          742k32285390
























              up vote
              0
              down vote













              You can do:



              IF ',' + 'abc,xyz' + ',' LIKE '%,' + @usrid + ',%'
              BEGIN
              SELECT 'TRUE';
              END


              The trick with the leading and trailing commas in both sides of the comparison serves two purposes: (1) to make sure '12' doesn't match '34,124' and (2) to catch when the one you're looking for is at the beginning and end of the string.



              And I wrote this this way:



              ',' + 'abc,xyz' + ','


              Because I assume you're not going to hard-code that big comma-separated string in your procedure and instead it's going to be derived from a column or a variable.



              In SQL Server 2016, you can instead do this:



              IF @usrid IN (SELECT Value FROM STRING_SPLIT('abc,xyz', ','))
              BEGIN
              SELECT 'TRUE';
              END


              Or just store your values in a table structure in the first place:



              DECLARE @values TABLE(Value varchar(32) PRIMARY KEY);
              INSERT @values VALUES('abc','xyz');

              IF @usrid IN (SELECT Value FROM @values)
              BEGIN
              SELECT 'TRUE';
              END


              Sets of individual values are much better in SQL Server than any kind of string or other structure that holds multiple values. Each piece of data should be its own fact. Combining them for convenience leads to pain at some point.






              share|improve this answer



























                up vote
                0
                down vote













                You can do:



                IF ',' + 'abc,xyz' + ',' LIKE '%,' + @usrid + ',%'
                BEGIN
                SELECT 'TRUE';
                END


                The trick with the leading and trailing commas in both sides of the comparison serves two purposes: (1) to make sure '12' doesn't match '34,124' and (2) to catch when the one you're looking for is at the beginning and end of the string.



                And I wrote this this way:



                ',' + 'abc,xyz' + ','


                Because I assume you're not going to hard-code that big comma-separated string in your procedure and instead it's going to be derived from a column or a variable.



                In SQL Server 2016, you can instead do this:



                IF @usrid IN (SELECT Value FROM STRING_SPLIT('abc,xyz', ','))
                BEGIN
                SELECT 'TRUE';
                END


                Or just store your values in a table structure in the first place:



                DECLARE @values TABLE(Value varchar(32) PRIMARY KEY);
                INSERT @values VALUES('abc','xyz');

                IF @usrid IN (SELECT Value FROM @values)
                BEGIN
                SELECT 'TRUE';
                END


                Sets of individual values are much better in SQL Server than any kind of string or other structure that holds multiple values. Each piece of data should be its own fact. Combining them for convenience leads to pain at some point.






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can do:



                  IF ',' + 'abc,xyz' + ',' LIKE '%,' + @usrid + ',%'
                  BEGIN
                  SELECT 'TRUE';
                  END


                  The trick with the leading and trailing commas in both sides of the comparison serves two purposes: (1) to make sure '12' doesn't match '34,124' and (2) to catch when the one you're looking for is at the beginning and end of the string.



                  And I wrote this this way:



                  ',' + 'abc,xyz' + ','


                  Because I assume you're not going to hard-code that big comma-separated string in your procedure and instead it's going to be derived from a column or a variable.



                  In SQL Server 2016, you can instead do this:



                  IF @usrid IN (SELECT Value FROM STRING_SPLIT('abc,xyz', ','))
                  BEGIN
                  SELECT 'TRUE';
                  END


                  Or just store your values in a table structure in the first place:



                  DECLARE @values TABLE(Value varchar(32) PRIMARY KEY);
                  INSERT @values VALUES('abc','xyz');

                  IF @usrid IN (SELECT Value FROM @values)
                  BEGIN
                  SELECT 'TRUE';
                  END


                  Sets of individual values are much better in SQL Server than any kind of string or other structure that holds multiple values. Each piece of data should be its own fact. Combining them for convenience leads to pain at some point.






                  share|improve this answer














                  You can do:



                  IF ',' + 'abc,xyz' + ',' LIKE '%,' + @usrid + ',%'
                  BEGIN
                  SELECT 'TRUE';
                  END


                  The trick with the leading and trailing commas in both sides of the comparison serves two purposes: (1) to make sure '12' doesn't match '34,124' and (2) to catch when the one you're looking for is at the beginning and end of the string.



                  And I wrote this this way:



                  ',' + 'abc,xyz' + ','


                  Because I assume you're not going to hard-code that big comma-separated string in your procedure and instead it's going to be derived from a column or a variable.



                  In SQL Server 2016, you can instead do this:



                  IF @usrid IN (SELECT Value FROM STRING_SPLIT('abc,xyz', ','))
                  BEGIN
                  SELECT 'TRUE';
                  END


                  Or just store your values in a table structure in the first place:



                  DECLARE @values TABLE(Value varchar(32) PRIMARY KEY);
                  INSERT @values VALUES('abc','xyz');

                  IF @usrid IN (SELECT Value FROM @values)
                  BEGIN
                  SELECT 'TRUE';
                  END


                  Sets of individual values are much better in SQL Server than any kind of string or other structure that holds multiple values. Each piece of data should be its own fact. Combining them for convenience leads to pain at some point.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 15:01

























                  answered Nov 10 at 14:55









                  Aaron Bertrand

                  205k27357401




                  205k27357401






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240019%2fhow-can-i-find-if-a-string-is-present-in-a-collection-of-comma-seprated-strings%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      The Sandy Post

                      Danny Elfman

                      Pages that link to "Head v. Amoskeag Manufacturing Co."