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'
sql
add a comment |
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'
sql
What version of SQL Server?
– Aaron Bertrand
Nov 10 at 14:54
add a comment |
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'
sql
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
asked Nov 10 at 14:40
Rehan Haque
206
206
What version of SQL Server?
– Aaron Bertrand
Nov 10 at 14:54
add a comment |
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
add a comment |
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)
add a comment |
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.
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 10 at 14:41
Gordon Linoff
742k32285390
742k32285390
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 15:01
answered Nov 10 at 14:55
Aaron Bertrand
205k27357401
205k27357401
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What version of SQL Server?
– Aaron Bertrand
Nov 10 at 14:54