question

Gogolo avatar image
Gogolo asked

Query where exists multiple values in same field

How to determine user ID from table where in same fields are stored two values, which can be stored like in explanation below?

DECLARE @prp_UserIds NVARCHAR(250);

SET @prp_UserIds = '30671,30955'

Parameters can be vise versa:

SET @prp_UserIds = '30955,30671'

sqlquery
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

user-1406 avatar image
user-1406 answered

You can use a table variable as below:

DECLARE @tab as table(UserID INT)

insert into @tab values(30671)

insert into @tab values(30955)


select UserID from @tab

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image
ThomasRushton answered

So, you want some sort of string split function to break the string field down into a table of separate values?

https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver16

...and then have a word with whoever designed this data model.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.