SQL : in clause in stored procedure:how to pass values - sql

I want to write a SQL Server 2005 stored procedure which will select and return the user records from the user table for some userids which are passed to the stored procedure as parameter.
How to do this ?
I can pass the user ids as a string separated by comma. So that I can use the
select *
from users
where userid in (userids)
E.g. : I want to select records for id's 5,6,7,8,9
How to write the stored procedure ?

For SQL Server 2005, check out Erland Sommarskog's excellent Arrays and Lists in SQL Server 2005 article which shows some techniques how to deal with lists and arrays in SQL Server 2005 (he also has another article for SQL Server 2000).
If you could upgrade to SQL Server 2008, you can use the new feature called "table valued parameter":
First, create a user-defined table type
CREATE TYPE dbo.MyUserIDs AS TABLE (UserID INT NOT NULL)
Secondly, use that table type in your stored procedure as a parameter:
CREATE PROC proc_GetUsers #UserIDTable MyUserIDs READONLY
AS
SELECT * FROM dbo.Users
WHERE userid IN (SELECT UserID FROM #UserIDTable)
See details here.
Marc

Just use it like this will work
Create procedure sp_DoctorList
#userid varchar(100)
as
begin
exec ('select * from doctor where userid in ( '+ #userid +' )')
end

you could use dynamic sql. Pass the in statement to a Sql SP via a variable and concatenate it into a query in the SQL and execute using sp_execute sql
create procedure myproc(#clause varchar(100)) as
begin
exec sp_executesql 'select * from users where userid in ( ' + #clause +' )'
end

see my previous answer to this
this is the best source:
http://www.sommarskog.se/arrays-in-sql.html
create a split function, and use it like:
SELECT
*
FROM YourTable y
INNER JOIN dbo.splitFunction(#Parameter) s ON y.ID=s.Value
I prefer the number table approach
For this method to work, you need to do this one time table setup:
SELECT TOP 10000 IDENTITY(int,1,1) AS Number
INTO Numbers
FROM sys.objects s1
CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
Once the Numbers table is set up, create this function:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
#SplitOn char(1) --REQUIRED, the character to split the #List string on
,#List varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN
(
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(#SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT #SplitOn + #List + #SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = #SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
);
GO
You can now easily split a CSV string into a table and join on it:
select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
OUTPUT:
ListValue
-----------------------
1
2
3
4
5
6777
(6 row(s) affected)
Your can pass in a CSV string into a procedure and process only rows for the given IDs:
SELECT
y.*
FROM YourTable y
INNER JOIN dbo.FN_ListToTable(',',#GivenCSV) s ON y.ID=s.ListValue

Assuming T-SQL, you can use this nice function (that returns a table).
DROP FUNCTION sp_ConvertStringToTable
GO
CREATE FUNCTION sp_ConvertStringToTable(#list ntext)
RETURNS #tbl TABLE (Position INT IDENTITY(1, 1) NOT NULL,
Value INT NOT NULL) AS
BEGIN
DECLARE #pos int,
#textpos int,
#chunklen smallint,
#str nvarchar(4000),
#tmpstr nvarchar(4000),
#leftover nvarchar(4000)
SET #textpos = 1
SET #leftover = ''
WHILE #textpos <= datalength(#list) / 2
BEGIN
SET #chunklen = 4000 - datalength(#leftover) / 2
SET #tmpstr = ltrim(#leftover + substring(#list, #textpos, #chunklen))
SET #textpos = #textpos + #chunklen
SET #pos = charindex(' ', #tmpstr)
WHILE #pos > 0
BEGIN
SET #str = substring(#tmpstr, 1, #pos - 1)
INSERT #tbl (Value) VALUES(convert(int, #str))
SET #tmpstr = ltrim(substring(#tmpstr, #pos + 1, len(#tmpstr)))
SET #pos = charindex(' ', #tmpstr)
END
SET #leftover = #tmpstr
END
IF ltrim(rtrim(#leftover)) <> ''
INSERT #tbl (Value) VALUES(convert(int, #leftover))
RETURN
END
GO
In this way:
SELECT * FROM Users
WHERE userid IN
( SELECT Value FROM sp_ConvertStringToTable('1 2 3') )
You can change the stored function to work with comma separated strings instead of space separated ones.
If you don't want / can't use a stored function you can include the code of it inside the stored procedure where needed.
EDIT: this is incredibly more performant than the string concatenation.

try this this works for me
DECLARE #InClause NVARCHAR(100)
SET #InClause = 'tom,dick,harry'
DECLARE #SafeInClause NVARCHAR(100)
SET #SafeInClause = ',' + #InClause + ','
SELECT * FROM myTable WHERE PATINDEX(',' + myColumn + ',', #SafeInClause) > 0

Quick and dirty..
CREATE PROCEDURE SelectUsers (#UserIds VARCHAR(8000))
AS
SELECT * FROM Users
WHERE userid IN (SELECT CONVERT(VARCHAR(8000), value) FROM STRING_SPLIT(#UserIds, ','))
EXEC SelectUsers #UserIds = 'a1b2,c3d4,e5f6'

You can also use Find_IN_SET instead of IN. See the query below
create procedure myproc(IN in_user_ids varchar(100))
begin
select * from users where FIND_IN_SET(userid, in_user_ids);
end

Related

Query stuff result left join on multiple ids [duplicate]

Duplicate of
Dynamic SQL Comma Delimited Value Query
Parameterized Queries with Like and In
I have a SQL Server Stored Procedure where I would like to pass a varchar full of comma delimited values to an IN function. For example:
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN (#Ids);
This does not work of course. I get the error:
Conversion failed when converting the varchar value '1,2,3,5,4,6,7,98,234' to data type int.
How can I accomplish this (or something relatively similar) without resorting to building dynamic SQL?
Of course if you're lazy like me, you could just do this:
Declare #Ids varchar(50) Set #Ids = ',1,2,3,5,4,6,7,98,234,'
Select * from sometable
where Charindex(','+cast(tableid as varchar(8000))+',', #Ids) > 0
Don't use a function that loops to split a string!, my function below will split a string very fast, with no looping!
Before you use my function, you need to set up a "helper" table, you only need to do this one time per database:
CREATE TABLE Numbers
(Number int NOT NULL,
CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
DECLARE #x int
SET #x=0
WHILE #x<8000
BEGIN
SET #x=#x+1
INSERT INTO Numbers VALUES (#x)
END
use this function to split your string, which does not loop and is very fast:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
#SplitOn char(1) --REQUIRED, the character to split the #List string on
,#List varchar(8000) --REQUIRED, the list to split apart
)
RETURNS
#ParsedList table
(
ListValue varchar(500)
)
AS
BEGIN
/**
Takes the given #List string and splits it apart based on the given #SplitOn character.
A table is returned, one row per split item, with a column name "ListValue".
This function workes for fixed or variable lenght items.
Empty and null items will not be included in the results set.
Returns a table, one row per item in the list, with a column name "ListValue"
EXAMPLE:
----------
SELECT * FROM dbo.FN_ListToTable(',','1,12,123,1234,54321,6,A,*,|||,,,,B')
returns:
ListValue
-----------
1
12
123
1234
54321
6
A
*
|||
B
(10 row(s) affected)
**/
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
INSERT INTO #ParsedList
(ListValue)
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(#SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT #SplitOn + #List + #SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = #SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
RETURN
END --Function FN_ListToTable
you can use this function as a table in a join:
SELECT
Col1, COl2, Col3...
FROM YourTable
INNER JOIN FN_ListToTable(',',#YourString) s ON YourTable.ID = s.ListValue
Here is your example:
Select * from sometable where tableid in(SELECT ListValue FROM dbo.FN_ListToTable(',',#Ids) s)
No Table No Function No Loop
Building on the idea of parsing your list into a table our DBA suggested using XML.
Declare #Ids varchar(50)
Set #Ids = ‘1,2,3,5,4,6,7,98,234’
DECLARE #XML XML
SET #XML = CAST('<i>' + REPLACE(#Ids, ',', '</i><i>') + '</i>' AS XML)
SELECT *
FROM
SomeTable
INNER JOIN #XML.nodes('i') x(i)
ON SomeTable .Id = x.i.value('.', 'VARCHAR(MAX)')
These seems to have the same performance as #KM's answer but, I think, a lot simpler.
You can create a function that returns a table.
so your statement would be something like
select * from someable
join Splitfunction(#ids) as splits on sometable.id = splits.id
Here is a simular function.
CREATE FUNCTION [dbo].[FUNC_SplitOrderIDs]
(
#OrderList varchar(500)
)
RETURNS
#ParsedList table
(
OrderID int
)
AS
BEGIN
DECLARE #OrderID varchar(10), #Pos int
SET #OrderList = LTRIM(RTRIM(#OrderList))+ ','
SET #Pos = CHARINDEX(',', #OrderList, 1)
IF REPLACE(#OrderList, ',', '') <> ''
BEGIN
WHILE #Pos > 0
BEGIN
SET #OrderID = LTRIM(RTRIM(LEFT(#OrderList, #Pos - 1)))
IF #OrderID <> ''
BEGIN
INSERT INTO #ParsedList (OrderID)
VALUES (CAST(#OrderID AS int)) --Use Appropriate conversion
END
SET #OrderList = RIGHT(#OrderList, LEN(#OrderList) - #Pos)
SET #Pos = CHARINDEX(',', #OrderList, 1)
END
END
RETURN
END
It's a very common question. Canned answer, several nice techniques:
http://www.sommarskog.se/arrays-in-sql-2005.html
This works perfectly! The below answers are too complicated. Don't look at this as dynamic. Set up your store procedure as follows:
(#id as varchar(50))
as
Declare #query as nvarchar(max)
set #query ='
select * from table
where id in('+#id+')'
EXECUTE sp_executesql #query
I can suggest using WITH like this:
DECLARE #Delim char(1) = ',';
SET #Ids = #Ids + #Delim;
WITH CTE(i, ls, id) AS (
SELECT 1, CHARINDEX(#Delim, #Ids, 1), SUBSTRING(#Ids, 1, CHARINDEX(#Delim, #Ids, 1) - 1)
UNION ALL
SELECT i + 1, CHARINDEX(#Delim, #Ids, ls + 1), SUBSTRING(#Ids, ls + 1, CHARINDEX(#Delim, #Ids, ls + 1) - CHARINDEX(#Delim, #Ids, ls) - 1)
FROM CTE
WHERE CHARINDEX(#Delim, #Ids, ls + 1) > 1
)
SELECT t.*
FROM yourTable t
INNER JOIN
CTE c
ON t.id = c.id;
I think a very simple solution could be following:
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE ','+#Ids+',' LIKE '%,'+CONVERT(VARCHAR(50),tableid)+',%';
Without using dynamic SQL, you have to take the input variable and use a split function to put the data into a temp table and then join to that.
Thanks, for your function I Used IT........................
This is my EXAMPLE
**UPDATE [RD].[PurchaseOrderHeader]
SET [DispatchCycleNumber] ='10'
WHERE OrderNumber in(select * FROM XA.fn_SplitOrderIDs(#InvoiceNumberList))**
CREATE FUNCTION [XA].[fn_SplitOrderIDs]
(
#OrderList varchar(500)
)
RETURNS
#ParsedList table
(
OrderID int
)
AS
BEGIN
DECLARE #OrderID varchar(10), #Pos int
SET #OrderList = LTRIM(RTRIM(#OrderList))+ ','
SET #Pos = CHARINDEX(',', #OrderList, 1)
IF REPLACE(#OrderList, ',', '') <> ''
BEGIN
WHILE #Pos > 0
BEGIN
SET #OrderID = LTRIM(RTRIM(LEFT(#OrderList, #Pos - 1)))
IF #OrderID <> ''
BEGIN
INSERT INTO #ParsedList (OrderID)
VALUES (CAST(#OrderID AS int)) --Use Appropriate conversion
END
SET #OrderList = RIGHT(#OrderList, LEN(#OrderList) - #Pos)
SET #Pos = CHARINDEX(',', #OrderList, 1)
END
END
RETURN
END
If you use SQL Server 2008 or higher, use table valued parameters; for example:
CREATE PROCEDURE [dbo].[GetAccounts](#accountIds nvarchar)
AS
BEGIN
SELECT *
FROM accountsTable
WHERE accountId IN (select * from #accountIds)
END
CREATE TYPE intListTableType AS TABLE (n int NOT NULL)
DECLARE #tvp intListTableType
-- inserts each id to one row in the tvp table
INSERT #tvp(n) VALUES (16509),(16685),(46173),(42925),(46167),(5511)
EXEC GetAccounts #tvp
Its been a while but I have done this in the past using XML as a interim.
I can't take any credit for this, but I'm afraid I no longer know where I got this idea from:
-- declare the variables needed
DECLARE #xml as xml,#str as varchar(100),#delimiter as varchar(10)
-- The string you want to split
SET #str='A,B,C,D,E,Bert,Ernie,1,2,3,4,5'
-- What you want to split on. Can be a single character or a string
SET #delimiter =','
-- Convert it to an XML document
SET #xml = cast(('<X>'+replace(#str,#delimiter ,'</X><X>')+'</X>') as xml)
-- Select back from the XML
SELECT N.value('.', 'varchar(10)') as value FROM #xml.nodes('X') as T(N)
Create a table function like below which parse comma separated varchar and returns a table that can be inner joined with other tables.
CREATE FUNCTION [dbo].[fn_SplitList]
(
#inString varchar(MAX) = '',
#inDelimiter char(1) = ',' -- Keep the delimiter to 100 chars or less. Generally a delimiter will be 1-2 chars only.
)
RETURNS #tbl_Return table
(
Unit varchar(1000) COLLATE Latin1_General_BIN
)
AS
BEGIN
INSERT INTO #tbl_Return
SELECT DISTINCT
LTRIM(RTRIM(piece.value('./text()[1]', 'varchar(1000)'))) COLLATE DATABASE_DEFAULT AS Unit
FROM
(
--
-- Replace any delimiters in the string with the "X" tag.
--
SELECT
CAST(('<X>' + REPLACE(s0.prsString, s0.prsSplitDelimit, '</X><X>') + '</X>') AS xml).query('.') AS units
FROM
(
--
-- Convert the string and delimiter into XML.
--
SELECT
(SELECT #inString FOR XML PATH('')) AS prsString,
(SELECT #inDelimiter FOR XML PATH('')) AS prsSplitDelimit
) AS s0
) AS s1
CROSS APPLY units.nodes('X') x(piece)
RETURN
END
=================================================
Now consume above created table function in your code,creation of function is one time activity in your database that can be used across databases as well on same server.
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT
*
FROM sometable AS st
INNER JOIN fn_SplitList(#ids, ',') AS sl
ON sl.unit = st.tableid
I've written a stored procedure to show how to do this before.
You basically have to process the string.
I tried to post the code here but the formatting got all screwy.
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[uspSplitTextList]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[uspSplitTextList]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-- uspSplitTextList
--
-- Description:
-- splits a separated list of text items and returns the text items
--
-- Arguments:
-- #list_text - list of text items
-- #Delimiter - delimiter
--
-- Notes:
-- 02/22/2006 - WSR : use DATALENGTH instead of LEN throughout because LEN doesn't count trailing blanks
--
-- History:
-- 02/22/2006 - WSR : revised algorithm to account for items crossing 8000 character boundary
-- 09/18/2006 - WSR : added to this project
--
CREATE PROCEDURE uspSplitTextList
#list_text text,
#Delimiter varchar(3)
AS
SET NOCOUNT ON
DECLARE #InputLen integer -- input text length
DECLARE #TextPos integer -- current position within input text
DECLARE #Chunk varchar(8000) -- chunk within input text
DECLARE #ChunkPos integer -- current position within chunk
DECLARE #DelimPos integer -- position of delimiter
DECLARE #ChunkLen integer -- chunk length
DECLARE #DelimLen integer -- delimiter length
DECLARE #ItemBegPos integer -- item starting position in text
DECLARE #ItemOrder integer -- item order in list
DECLARE #DelimChar varchar(1) -- first character of delimiter (simple delimiter)
-- create table to hold list items
-- actually their positions because we may want to scrub this list eliminating bad entries before substring is applied
CREATE TABLE #list_items ( item_order integer, item_begpos integer, item_endpos integer )
-- process list
IF #list_text IS NOT NULL
BEGIN
-- initialize
SET #InputLen = DATALENGTH(#list_text)
SET #TextPos = 1
SET #DelimChar = SUBSTRING(#Delimiter, 1, 1)
SET #DelimLen = DATALENGTH(#Delimiter)
SET #ItemBegPos = 1
SET #ItemOrder = 1
SET #ChunkLen = 1
-- cycle through input processing chunks
WHILE #TextPos <= #InputLen AND #ChunkLen <> 0
BEGIN
-- get current chunk
SET #Chunk = SUBSTRING(#list_text, #TextPos, 8000)
-- setup initial variable values
SET #ChunkPos = 1
SET #ChunkLen = DATALENGTH(#Chunk)
SET #DelimPos = CHARINDEX(#DelimChar, #Chunk, #ChunkPos)
-- loop over the chunk, until the last delimiter
WHILE #ChunkPos <= #ChunkLen AND #DelimPos <> 0
BEGIN
-- see if this is a full delimiter
IF SUBSTRING(#list_text, (#TextPos + #DelimPos - 1), #DelimLen) = #Delimiter
BEGIN
-- insert position
INSERT INTO #list_items (item_order, item_begpos, item_endpos)
VALUES (#ItemOrder, #ItemBegPos, (#TextPos + #DelimPos - 1) - 1)
-- adjust positions
SET #ItemOrder = #ItemOrder + 1
SET #ItemBegPos = (#TextPos + #DelimPos - 1) + #DelimLen
SET #ChunkPos = #DelimPos + #DelimLen
END
ELSE
BEGIN
-- adjust positions
SET #ChunkPos = #DelimPos + 1
END
-- find next delimiter
SET #DelimPos = CHARINDEX(#DelimChar, #Chunk, #ChunkPos)
END
-- adjust positions
SET #TextPos = #TextPos + #ChunkLen
END
-- handle last item
IF #ItemBegPos <= #InputLen
BEGIN
-- insert position
INSERT INTO #list_items (item_order, item_begpos, item_endpos)
VALUES (#ItemOrder, #ItemBegPos, #InputLen)
END
-- delete the bad items
DELETE FROM #list_items
WHERE item_endpos < item_begpos
-- return list items
SELECT SUBSTRING(#list_text, item_begpos, (item_endpos - item_begpos + 1)) AS item_text, item_order, item_begpos, item_endpos
FROM #list_items
ORDER BY item_order
END
DROP TABLE #list_items
RETURN
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
I have same idea with user KM. but do not need extra table Number. Just this function only.
CREATE FUNCTION [dbo].[FN_ListToTable]
(
#SplitOn char(1) --REQUIRED, the character to split the #List string on
,#List varchar(8000) --REQUIRED, the list to split apart
)
RETURNS
#ParsedList table
(
ListValue varchar(500)
)
AS
BEGIN
DECLARE #number int = 0
DECLARE #childString varchar(502) = ''
DECLARE #lengthChildString int = 0
DECLARE #processString varchar(502) = #SplitOn + #List + #SplitOn
WHILE #number < LEN(#processString)
BEGIN
SET #number = #number + 1
SET #lengthChildString = CHARINDEX(#SplitOn, #processString, #number + 1) - #number - 1
IF #lengthChildString > 0
BEGIN
SET #childString = LTRIM(RTRIM(SUBSTRING(#processString, #number + 1, #lengthChildString)))
IF #childString IS NOT NULL AND #childString != ''
BEGIN
INSERT INTO #ParsedList(ListValue) VALUES (#childString)
SET #number = #number + #lengthChildString - 1
END
END
END
RETURN
END
And here is the test:
SELECT ListValue FROM dbo.FN_ListToTable('/','a/////bb/c')
Result:
ListValue
______________________
a
bb
c
-- select * from dbo.Split_ID('77,106')
ALTER FUNCTION dbo.Split_ID(#String varchar(8000))
returns #temptable TABLE (ID varchar(8000))
as
begin
declare #idx int
declare #slice varchar(8000)
declare #Delimiter char(1)
set #Delimiter =','
select #idx = 1
if len(#String)<1 or #String is null return
while #idx!= 0
begin
set #idx = charindex(#Delimiter,#String)
if #idx!=0
set #slice = left(#String,#idx - 1)
else
set #slice = #String
if(len(#slice)>0)
insert into #temptable(ID) values(#slice)
set #String = right(#String,len(#String) - #idx)
if len(#String) = 0 break
end
return
end
You could do it like this:
create or replace
PROCEDURE UDP_SETBOOKMARK
(
P_USERID IN VARCHAR2
, P_BOOKMARK IN VARCHAR2
) AS
BEGIN
UPDATE T_ER_Bewertung
SET LESEZEICHEN = P_BOOKMARK
WHERE STAMM_ID in( select regexp_substr(P_USERID,'[^,]+', 1, level) from dual
connect by regexp_substr(P_USERID, '[^,]+', 1, level) is not null )
and ER_ID = (select max(ER_ID) from T_ER_Bewertung_Kopie);
commit;
END UDP_SETBOOKMARK;
Then try it with
Begin
UDP_SETBOOKMARK ('1,2,3,4,5', 'Test');
End;
You can use this IN-Clause with regexp_substr in other situations too, just try it.
Error 493: The column 'i' that was returned from the nodes() method cannot be
used directly. It can only be used with one of the four XML data type
methods, exist(), nodes(), query(), and value(), or in IS NULL and IS NOT
NULL checks.
The above errorr was fixed in SQL Server 2014 by using following snippet
Declare #Ids varchar(50)
Set #Ids = '1,2,3,5,4,6,7,98,234'
DECLARE #XML XML
SET #XML = CAST('<i>' + REPLACE(#Ids, ',', '</i><i>') + '</i>' AS XML)
SELECT SomeTable.*
FROM
SomeTable
cross apply #XML.nodes('i') x(i)
where SomeTable .Id = x.i.value('.', 'VARCHAR(MAX)')
Try this:
SELECT ProductId, Name, Tags
FROM Product
WHERE '1,2,3,' LIKE '%' + CAST(ProductId AS VARCHAR(20)) + ',%';
As said on the last example of this link
Tons of answers here, but to add my two cents I think STRING_SPLIT is a very simple approach to this sort of problem:
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN;
(SELECT value FROM STRING_SPLIT(#Ids, ','))
Best and simple approach.
DECLARE #AccumulateKeywordCopy NVARCHAR(2000),#IDDupCopy NVARCHAR(50);
SET #AccumulateKeywordCopy ='';
SET #IDDupCopy ='';
SET #IDDup = (SELECT CONVERT(VARCHAR(MAX), <columnName>) FROM <tableName> WHERE <clause>)
SET #AccumulateKeywordCopy = ','+#AccumulateKeyword+',';
SET #IDDupCopy = ','+#IDDup +',';
SET #IDDupCheck = CHARINDEX(#IDDupCopy,#AccumulateKeywordCopy)
CREATE TABLE t
(
id INT,
col1 VARCHAR(50)
)
INSERT INTO t
VALUES (1,
'param1')
INSERT INTO t
VALUES (2,
'param2')
INSERT INTO t
VALUES (3,
'param3')
INSERT INTO t
VALUES (4,
'param4')
INSERT INTO t
VALUES (5,
'param5')
DECLARE #params VARCHAR(100)
SET #params = ',param1,param2,param3,'
SELECT *
FROM t
WHERE Charindex(',' + Cast(col1 AS VARCHAR(8000)) + ',', #params) > 0
working fiddle find here Fiddle
I ran into the same issue, and I don't want to have any footprint on the source database - i.e. no stored procedures or functions. I went about it this way:
declare #IDs table (Value int)
insert into #IDs values(1)
insert into #IDs values(2)
insert into #IDs values(3)
insert into #IDs values(5)
insert into #IDs values(4)
insert into #IDs values(6)
insert into #IDs values(7)
insert into #IDs values(98)
insert into #IDs values(234)
SELECT *
FROM sometable
WHERE tableid IN (select Value from #IDs)
The answer by #RBarryYoung (above) worked for me.
But when you have spaces in between the comma separated string values, then it will omit IDs with spaces. So I removed the spaces.
Take a look at the code snippet below.
Declare #Ids varchar(50) Set #Ids = '1 , 2,3'
set #Ids=','+Replace(#Ids,' ', '')+',';
Select * from [tblEmployee]
where Charindex(','+cast(ID as varchar(8000))+',', #Ids) > 0
WHERE someId IN (SELECT convert(int, value) FROM string_split(#stringOfCommaDelimitedIds, ','))
The simplest way i found was to use FIND_IN_SET
FIND_IN_SET(column_name, values)
values=(1,2,3)
SELECT name WHERE FIND_IN_SET(id, values)
This came in handy for one of my requirements where I did not want to use CTE and also did not want to go with the inner join.
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT
cn1,cn2,cn3
FROM tableName
WHERE columnName in (select Value from fn_SplitString(#ids, ','))
Function for split string :
CREATE FUNCTION [dbo].[fn_SplitString] ( #stringToSplit VARCHAR(MAX), #seperator Char )
RETURNS
#returnList TABLE ([Value] [nvarchar] (500))
AS
BEGIN
DECLARE #name NVARCHAR(255)
DECLARE #pos INT
WHILE CHARINDEX(#seperator, #stringToSplit) > 0
BEGIN
SELECT #pos = CHARINDEX(#seperator, #stringToSplit)
SELECT #name = SUBSTRING(#stringToSplit, 1, #pos-1)
INSERT INTO #returnList
SELECT #name
SELECT #stringToSplit = SUBSTRING(#stringToSplit, #pos+1, LEN(#stringToSplit)-#pos)
END
INSERT INTO #returnList
SELECT #stringToSplit
RETURN
END

How to exclude one column from result? [duplicate]

This question already has answers here:
Exclude a column using SELECT * [except columnA] FROM tableA?
(47 answers)
Closed 7 years ago.
I have a table with columns
a,b,c,d,e,f,g,...,z
Is it possible to write a simple query that will gives all columns except a without manualy specifing all other columns?
something that will be equivelant to:
Select b,c,d,e,f,g,h,...z
from TableG
To answer your question, you cannot do that directly, HOWEVER i found a solution for you.
The SELECT statement of SQL when using Physical table can only do SELECT * that will return all columns and SELECT Column1, Column2, Column3... for specific columns, there is no WHERE condition that will exclude 1 column name in the SELECT statement of SQL. How ever you can manipulate the table and the data the way you wanted it using temporary table
Solution:
Insert into temp table
Drop the column that you want to exclude from the temp table
Select all data from temp table.
SELECT * INTO #TempTable
FROM TableG
ALTER TABLE #TempTable
DROP COLUMN a
SELECT * FROM #TempTable
DROP TABLE #TempTable
I found the solution here: SQL exclude a column using SELECT * [except columnA] FROM tableA?
/************************************************************
Function To Split Strings
************************************************************/
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.SplitString') IS NOT NULL
BEGIN
DROP FUNCTION dbo.SplitString
END
GO
create function [dbo].[SplitString]
(
#String varchar(max),
#Delimiter char(1)
)
returns #SplittedValues table
(
str_item varchar(100) primary key
)
as
begin
declare #SplitLength int
while len(#String) > 0
begin
select #SplitLength = (case charindex(#Delimiter,#String) when 0 then
len(#String) else charindex(#Delimiter,#String) -1 end)
insert into #SplittedValues
select substring(#String,1,#SplitLength)
select #String = (case (len(#String) - #SplitLength) when 0 then ''
else right(#String, len(#String) - #SplitLength - 1) end)
end
return
end
GO
/************************************************************
Function To Get columns names excluding some of them
************************************************************/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.get_columns') IS NOT NULL
BEGIN
DROP FUNCTION dbo.get_columns
END
GO
CREATE FUNCTION dbo.get_columns
(
#table_name varchar(100),
#excluded_column_names varchar(100),
#delimter char(1)
)
RETURNS varchar(4000)
AS
BEGIN
declare #cols varchar(4000)
select #cols = COALESCE(#cols+',' ,'') + name
from sys.columns
where object_id=object_id(#table_name)
and name not in (select str_item from dbo.SplitString(#excluded_column_names,#delimter))
return #cols
END
GO
/************************************************************
Function To Get columns names excluding some of them
************************************************************/
declare #qry nvarchar(max)
set #qry = ' select ' + dbo.get_columns('TableName','Exclude_col_1,Exclude_col_2,Exclude_col_3',',')
+ ' from TableName'
+ ' where condition'
EXECUTE sp_executesql #qry
GO

Building dynamic T-SQL query from a string argument in a sproc

Let's say I have a table which contains a varchar field:
CREATE TABLE [MyTable] (
[MyId] varchar(3) NOT NULL,
.....
)
The [MyId] column contains sequential alphanum values like A1, A2... A99, B1, B2..B99, C1 and so on (up to Z99).
What I'd like to do is to extract rows from the table whose MyId field matches some specific prefixes... e.g. I'd like to fetch rows from the series A, C, P and X.
And I'd like to this with a sproc which will dynamically construct the query based on the prefix alphabets supplied in the argument.
I'm thinking about something like this...
CREATE PROCEDURE [dbo].[uspFilterMyTable]
#prefixArray varchar(max)
AS
... -- split individual characters from #prefixArray into an array
SELECT * FROM [MyTable]
WHERE
[MyId] LIKE ....
OR
[MyId] LIKE .... -- iterate all characters from #prefixArray
I think the main bulk of the stored procedure will resemble the following pseudo-code:
DECLARE #sql nvarchar(max)
-- iterate through all the characters
SET #sql = 'SELECT * FROM [MyTable] WHERE [MyId] LIKE ' + #charInTheArray + '%'
SET #sql = #sql + ' OR [MyId] LIKE ' + #nextCharInArray + '%'
EXEC (#sql)
The above proecedure will be called like this:
EXEC uspFilterMyTable("A,C,P,X")
... or perhaps like this (if it makes splitting the alphabets easier):
EXEC uspFilterMyTable("ACPX")
Any ideas? Pointers?
Update: OK, this is what I've come up with ([Split] function borrowed from Chhatrapati Sharma):
-- [MyTable] contains these rows: 'A7', 'A87', 'B16', 'C51', 'H99', 'X12'
-- the "input" parameter
DECLARE #prefixArray NVARCHAR(100)= 'H,A,C'
-- split the string into SQL wild-card patterns
DECLARE charCursor CURSOR FOR
select items + N'%' from dbo.Split(#prefixArray, ',')
OPEN charCursor;
DECLARE #pattern CHAR(2)
-- create temp table if necessary
IF NOT EXISTS(SELECT * FROM TEMPDB.SYS.TABLES WHERE NAME LIKE '#tmpTable%')
CREATE TABLE #tmpTable ([Id] VARCHAR(3) NOT NULL)
-- purge old data
DELETE FROM #tmpTable
FETCH NEXT FROM charCursor into #pattern
WHILE ##FETCH_STATUS = 0
BEGIN
--SELECT * INTO #tmpTable FROM [MyTable] WHERE [MyId] LIKE #pattern
Insert Into #tmpTable Select * FROM [MyTable] WHERE [MyId] LIKE #pattern
FETCH NEXT FROM charCursor into #pattern
END
CLOSE charCursor;
DEALLOCATE charCursor;
-- return the values
SELECT * FROM #tmpTable
It's ugly I know, but it works... any tips to improvise the code?
first you should create below function and then use this in query like this
SELECT * FROM [MyTable] WHERE [MyId] in (select items from dbo.split(#prefixArray,','))
CREATE FUNCTION [dbo].[Split](#String varchar(8000), #Delimiter char(1))
returns #temptable TABLE (items varchar(8000))
as
begin
declare #idx int
declare #slice varchar(8000)
select #idx = 1
if len(#String)<1 or #String is null return
while #idx!= 0
begin
set #idx = charindex(#Delimiter,#String)
if #idx!=0
set #slice = left(#String,#idx - 1)
else
set #slice = #String
if(len(#slice)>0)
insert into #temptable(Items) values(#slice)
set #String = right(#String,len(#String) - #idx)
if len(#String) = 0 break
end
return
end
Here you have a nice and fast split method based on XML:
DECLARE #str NVARCHAR(100)= 'A1,B3,C4,B12,K19', #separator VARCHAR(1)= ','
DECLARE #SplitedList TABLE (code NVARCHAR(30))
DECLARE #XMLList XML
SET #XMLList=CAST('<i>'+REPLACE(#str, #separator,'</i><i>')+'</i>' AS XML)
INSERT INTO #SplitedList
SELECT x.i.value('(./text())[1]','varchar(100)')
FROM #XMLList.nodes('i') x(i)
SELECT * FROM #SplitedList
Result will be a table with the splitted values:
code
A1
B3
C4
B12
K19
From here you can continue and use this table on your procedure and join with you original table using LIKE as you propossed.
I would have suggested you to use table valued parameters to call your stored procedure. I guess you call it from .net. But EF I think will not be able to handle it, though you might check it. If not, I think the best way is to first parse the string into a temporary table, or a table value and after that join with it.
With TVP:
CREATE PROCEDURE [dbo].[uspFilterMyTable]
#prefixArray tvp_idlist readonly
as
select
t.*
from MyTable t
join #prefixArray pa on pa.id = t.myid
With a split function (of your choosing, you find many examples on the net)
CREATE PROCEDURE [dbo].[uspFilterMyTable]
#prefixArray varchar(max)
as
create #prefixArray tvp_idlist
insert into #prefixArray (id)
select id from dbo.myCustomSplit(#prefixArray,',')
select
t.*
from MyTable t
join #prefixArray pa on pa.id = t.myid
Where for both cases #prefixArray is a table variable is Id = varchar(3)
As an edit, after a little digging, it seems that with a little work EF works fine with TVPs. Check this : Entity Framework Stored Procedure Table Value Parameter. So The best thing is to send directly a table to your stored procedure, then to send a string to parse.

stored proc - executing a query with NOT IN where clause

i have a stored procedure
Create PROCEDURE abc
#sRemovePreviouslySelectedWhereClause nvarchar(max)
AS
BEGIN
SELECT *
FROM table
WHERE nId NOT IN (#sRemovePreviouslySelectedWhereClause)
END;
The parameter #sRemovePreviouslySelectedWhereClause can have values like 0,1 . But this fails with error message:
Conversion failed when converting the nvarchar value ' 0,1 ' to data type int.
Is there any other way to achieve this other than dynamic queries?
First, create a split function which splits your delimited string into a table:
CREATE FUNCTION [dbo].[Split]
(
#String varchar(max)
,#Delimiter char
)
RETURNS #Results table
(
Ordinal int
,StringValue varchar(max)
)
as
begin
set #String = isnull(#String,'')
set #Delimiter = isnull(#Delimiter,'')
declare
#TempString varchar(max) = #String
,#Ordinal int = 0
,#CharIndex int = 0
set #CharIndex = charindex(#Delimiter, #TempString)
while #CharIndex != 0 begin
set #Ordinal += 1
insert #Results values
(
#Ordinal
,substring(#TempString, 0, #CharIndex)
)
set #TempString = substring(#TempString, #CharIndex + 1, len(#TempString) - #CharIndex)
set #CharIndex = charindex(#Delimiter, #TempString)
end
if #TempString != '' begin
set #Ordinal += 1
insert #Results values
(
#Ordinal
,#TempString
)
end
return
end
Then change your where clause as follows:
select
t.*
from [yourTable] t
where t.[ID] not in (select cast([StringValue] as int) from dbo.Split(#sRemovePreviouslySelectedWhereClause,','))
Create FUNCTION [dbo].[fn_Split] (
#List nvarchar(2000), #SplitOn nvarchar(5)
)
RETURNS #RtnValue table (
Value nvarchar(100) )
AS
BEGIN
While (Charindex(#SplitOn,#List)>0)
Begin
Insert Into #RtnValue (value)
Select Value = ltrim(rtrim(Substring(#List,1,Charindex(#SplitOn,#List)-1)))
Set #List = Substring(#List,Charindex(#SplitOn,#List)+len(#SplitOn),len(#List))
End
Insert Into #RtnValue (Value)
Select Value = ltrim(rtrim(#List))
Return
END
**********
Create PROCEDURE abc
#sRemovePreviouslySelectedWhereClause nvarchar(max)
AS
BEGIN
SELECT *
FROM Table
WHERE nId NOT IN (select * from dbo.fn_Split(#sRemovePreviouslySelectedWhereClause,','))
END;
You have to split the #sRemovePreviouslySelectedWhereClause parameter by ',' and place the resulting values in a temp table. Then your select would look like
select * from table where nId not in (select nId from #tempIds)
This approach you're trying doesn't work. But if you're on SQL Server 2008, you could make use of the new features called Table Valued Parameters.
Basically, you create a user-defined table type
CREATE TYPE dbo.nIdTable AS TABLE(nID INT)
and you can then pass in multiple values in that TVP from the outside (e.g. from ADO.NET or such):
CREATE PROCEDURE abc(#idValues dbo.nIdTable READONLY)
and use that table variable inside your stored proc:
SELECT *
FROM table
WHERE nId NOT IN (SELECT nID FROM #idValues)
You will need to use Dynamic sql for such kind of queries.
first construct the query and
SET #sql = 'select * from table
where nId not in (' + #sRemovePreviouslySelectedWhereClause+ ')'
then use EXEC(#sql) to run the query.

Passing a varchar full of comma delimited values to a SQL Server IN function

Duplicate of
Dynamic SQL Comma Delimited Value Query
Parameterized Queries with Like and In
I have a SQL Server Stored Procedure where I would like to pass a varchar full of comma delimited values to an IN function. For example:
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN (#Ids);
This does not work of course. I get the error:
Conversion failed when converting the varchar value '1,2,3,5,4,6,7,98,234' to data type int.
How can I accomplish this (or something relatively similar) without resorting to building dynamic SQL?
Of course if you're lazy like me, you could just do this:
Declare #Ids varchar(50) Set #Ids = ',1,2,3,5,4,6,7,98,234,'
Select * from sometable
where Charindex(','+cast(tableid as varchar(8000))+',', #Ids) > 0
Don't use a function that loops to split a string!, my function below will split a string very fast, with no looping!
Before you use my function, you need to set up a "helper" table, you only need to do this one time per database:
CREATE TABLE Numbers
(Number int NOT NULL,
CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
DECLARE #x int
SET #x=0
WHILE #x<8000
BEGIN
SET #x=#x+1
INSERT INTO Numbers VALUES (#x)
END
use this function to split your string, which does not loop and is very fast:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
#SplitOn char(1) --REQUIRED, the character to split the #List string on
,#List varchar(8000) --REQUIRED, the list to split apart
)
RETURNS
#ParsedList table
(
ListValue varchar(500)
)
AS
BEGIN
/**
Takes the given #List string and splits it apart based on the given #SplitOn character.
A table is returned, one row per split item, with a column name "ListValue".
This function workes for fixed or variable lenght items.
Empty and null items will not be included in the results set.
Returns a table, one row per item in the list, with a column name "ListValue"
EXAMPLE:
----------
SELECT * FROM dbo.FN_ListToTable(',','1,12,123,1234,54321,6,A,*,|||,,,,B')
returns:
ListValue
-----------
1
12
123
1234
54321
6
A
*
|||
B
(10 row(s) affected)
**/
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
INSERT INTO #ParsedList
(ListValue)
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(#SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT #SplitOn + #List + #SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = #SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
RETURN
END --Function FN_ListToTable
you can use this function as a table in a join:
SELECT
Col1, COl2, Col3...
FROM YourTable
INNER JOIN FN_ListToTable(',',#YourString) s ON YourTable.ID = s.ListValue
Here is your example:
Select * from sometable where tableid in(SELECT ListValue FROM dbo.FN_ListToTable(',',#Ids) s)
No Table No Function No Loop
Building on the idea of parsing your list into a table our DBA suggested using XML.
Declare #Ids varchar(50)
Set #Ids = ‘1,2,3,5,4,6,7,98,234’
DECLARE #XML XML
SET #XML = CAST('<i>' + REPLACE(#Ids, ',', '</i><i>') + '</i>' AS XML)
SELECT *
FROM
SomeTable
INNER JOIN #XML.nodes('i') x(i)
ON SomeTable .Id = x.i.value('.', 'VARCHAR(MAX)')
These seems to have the same performance as #KM's answer but, I think, a lot simpler.
You can create a function that returns a table.
so your statement would be something like
select * from someable
join Splitfunction(#ids) as splits on sometable.id = splits.id
Here is a simular function.
CREATE FUNCTION [dbo].[FUNC_SplitOrderIDs]
(
#OrderList varchar(500)
)
RETURNS
#ParsedList table
(
OrderID int
)
AS
BEGIN
DECLARE #OrderID varchar(10), #Pos int
SET #OrderList = LTRIM(RTRIM(#OrderList))+ ','
SET #Pos = CHARINDEX(',', #OrderList, 1)
IF REPLACE(#OrderList, ',', '') <> ''
BEGIN
WHILE #Pos > 0
BEGIN
SET #OrderID = LTRIM(RTRIM(LEFT(#OrderList, #Pos - 1)))
IF #OrderID <> ''
BEGIN
INSERT INTO #ParsedList (OrderID)
VALUES (CAST(#OrderID AS int)) --Use Appropriate conversion
END
SET #OrderList = RIGHT(#OrderList, LEN(#OrderList) - #Pos)
SET #Pos = CHARINDEX(',', #OrderList, 1)
END
END
RETURN
END
It's a very common question. Canned answer, several nice techniques:
http://www.sommarskog.se/arrays-in-sql-2005.html
This works perfectly! The below answers are too complicated. Don't look at this as dynamic. Set up your store procedure as follows:
(#id as varchar(50))
as
Declare #query as nvarchar(max)
set #query ='
select * from table
where id in('+#id+')'
EXECUTE sp_executesql #query
I can suggest using WITH like this:
DECLARE #Delim char(1) = ',';
SET #Ids = #Ids + #Delim;
WITH CTE(i, ls, id) AS (
SELECT 1, CHARINDEX(#Delim, #Ids, 1), SUBSTRING(#Ids, 1, CHARINDEX(#Delim, #Ids, 1) - 1)
UNION ALL
SELECT i + 1, CHARINDEX(#Delim, #Ids, ls + 1), SUBSTRING(#Ids, ls + 1, CHARINDEX(#Delim, #Ids, ls + 1) - CHARINDEX(#Delim, #Ids, ls) - 1)
FROM CTE
WHERE CHARINDEX(#Delim, #Ids, ls + 1) > 1
)
SELECT t.*
FROM yourTable t
INNER JOIN
CTE c
ON t.id = c.id;
I think a very simple solution could be following:
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE ','+#Ids+',' LIKE '%,'+CONVERT(VARCHAR(50),tableid)+',%';
Without using dynamic SQL, you have to take the input variable and use a split function to put the data into a temp table and then join to that.
Thanks, for your function I Used IT........................
This is my EXAMPLE
**UPDATE [RD].[PurchaseOrderHeader]
SET [DispatchCycleNumber] ='10'
WHERE OrderNumber in(select * FROM XA.fn_SplitOrderIDs(#InvoiceNumberList))**
CREATE FUNCTION [XA].[fn_SplitOrderIDs]
(
#OrderList varchar(500)
)
RETURNS
#ParsedList table
(
OrderID int
)
AS
BEGIN
DECLARE #OrderID varchar(10), #Pos int
SET #OrderList = LTRIM(RTRIM(#OrderList))+ ','
SET #Pos = CHARINDEX(',', #OrderList, 1)
IF REPLACE(#OrderList, ',', '') <> ''
BEGIN
WHILE #Pos > 0
BEGIN
SET #OrderID = LTRIM(RTRIM(LEFT(#OrderList, #Pos - 1)))
IF #OrderID <> ''
BEGIN
INSERT INTO #ParsedList (OrderID)
VALUES (CAST(#OrderID AS int)) --Use Appropriate conversion
END
SET #OrderList = RIGHT(#OrderList, LEN(#OrderList) - #Pos)
SET #Pos = CHARINDEX(',', #OrderList, 1)
END
END
RETURN
END
If you use SQL Server 2008 or higher, use table valued parameters; for example:
CREATE PROCEDURE [dbo].[GetAccounts](#accountIds nvarchar)
AS
BEGIN
SELECT *
FROM accountsTable
WHERE accountId IN (select * from #accountIds)
END
CREATE TYPE intListTableType AS TABLE (n int NOT NULL)
DECLARE #tvp intListTableType
-- inserts each id to one row in the tvp table
INSERT #tvp(n) VALUES (16509),(16685),(46173),(42925),(46167),(5511)
EXEC GetAccounts #tvp
Its been a while but I have done this in the past using XML as a interim.
I can't take any credit for this, but I'm afraid I no longer know where I got this idea from:
-- declare the variables needed
DECLARE #xml as xml,#str as varchar(100),#delimiter as varchar(10)
-- The string you want to split
SET #str='A,B,C,D,E,Bert,Ernie,1,2,3,4,5'
-- What you want to split on. Can be a single character or a string
SET #delimiter =','
-- Convert it to an XML document
SET #xml = cast(('<X>'+replace(#str,#delimiter ,'</X><X>')+'</X>') as xml)
-- Select back from the XML
SELECT N.value('.', 'varchar(10)') as value FROM #xml.nodes('X') as T(N)
Create a table function like below which parse comma separated varchar and returns a table that can be inner joined with other tables.
CREATE FUNCTION [dbo].[fn_SplitList]
(
#inString varchar(MAX) = '',
#inDelimiter char(1) = ',' -- Keep the delimiter to 100 chars or less. Generally a delimiter will be 1-2 chars only.
)
RETURNS #tbl_Return table
(
Unit varchar(1000) COLLATE Latin1_General_BIN
)
AS
BEGIN
INSERT INTO #tbl_Return
SELECT DISTINCT
LTRIM(RTRIM(piece.value('./text()[1]', 'varchar(1000)'))) COLLATE DATABASE_DEFAULT AS Unit
FROM
(
--
-- Replace any delimiters in the string with the "X" tag.
--
SELECT
CAST(('<X>' + REPLACE(s0.prsString, s0.prsSplitDelimit, '</X><X>') + '</X>') AS xml).query('.') AS units
FROM
(
--
-- Convert the string and delimiter into XML.
--
SELECT
(SELECT #inString FOR XML PATH('')) AS prsString,
(SELECT #inDelimiter FOR XML PATH('')) AS prsSplitDelimit
) AS s0
) AS s1
CROSS APPLY units.nodes('X') x(piece)
RETURN
END
=================================================
Now consume above created table function in your code,creation of function is one time activity in your database that can be used across databases as well on same server.
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT
*
FROM sometable AS st
INNER JOIN fn_SplitList(#ids, ',') AS sl
ON sl.unit = st.tableid
I've written a stored procedure to show how to do this before.
You basically have to process the string.
I tried to post the code here but the formatting got all screwy.
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[uspSplitTextList]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[uspSplitTextList]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-- uspSplitTextList
--
-- Description:
-- splits a separated list of text items and returns the text items
--
-- Arguments:
-- #list_text - list of text items
-- #Delimiter - delimiter
--
-- Notes:
-- 02/22/2006 - WSR : use DATALENGTH instead of LEN throughout because LEN doesn't count trailing blanks
--
-- History:
-- 02/22/2006 - WSR : revised algorithm to account for items crossing 8000 character boundary
-- 09/18/2006 - WSR : added to this project
--
CREATE PROCEDURE uspSplitTextList
#list_text text,
#Delimiter varchar(3)
AS
SET NOCOUNT ON
DECLARE #InputLen integer -- input text length
DECLARE #TextPos integer -- current position within input text
DECLARE #Chunk varchar(8000) -- chunk within input text
DECLARE #ChunkPos integer -- current position within chunk
DECLARE #DelimPos integer -- position of delimiter
DECLARE #ChunkLen integer -- chunk length
DECLARE #DelimLen integer -- delimiter length
DECLARE #ItemBegPos integer -- item starting position in text
DECLARE #ItemOrder integer -- item order in list
DECLARE #DelimChar varchar(1) -- first character of delimiter (simple delimiter)
-- create table to hold list items
-- actually their positions because we may want to scrub this list eliminating bad entries before substring is applied
CREATE TABLE #list_items ( item_order integer, item_begpos integer, item_endpos integer )
-- process list
IF #list_text IS NOT NULL
BEGIN
-- initialize
SET #InputLen = DATALENGTH(#list_text)
SET #TextPos = 1
SET #DelimChar = SUBSTRING(#Delimiter, 1, 1)
SET #DelimLen = DATALENGTH(#Delimiter)
SET #ItemBegPos = 1
SET #ItemOrder = 1
SET #ChunkLen = 1
-- cycle through input processing chunks
WHILE #TextPos <= #InputLen AND #ChunkLen <> 0
BEGIN
-- get current chunk
SET #Chunk = SUBSTRING(#list_text, #TextPos, 8000)
-- setup initial variable values
SET #ChunkPos = 1
SET #ChunkLen = DATALENGTH(#Chunk)
SET #DelimPos = CHARINDEX(#DelimChar, #Chunk, #ChunkPos)
-- loop over the chunk, until the last delimiter
WHILE #ChunkPos <= #ChunkLen AND #DelimPos <> 0
BEGIN
-- see if this is a full delimiter
IF SUBSTRING(#list_text, (#TextPos + #DelimPos - 1), #DelimLen) = #Delimiter
BEGIN
-- insert position
INSERT INTO #list_items (item_order, item_begpos, item_endpos)
VALUES (#ItemOrder, #ItemBegPos, (#TextPos + #DelimPos - 1) - 1)
-- adjust positions
SET #ItemOrder = #ItemOrder + 1
SET #ItemBegPos = (#TextPos + #DelimPos - 1) + #DelimLen
SET #ChunkPos = #DelimPos + #DelimLen
END
ELSE
BEGIN
-- adjust positions
SET #ChunkPos = #DelimPos + 1
END
-- find next delimiter
SET #DelimPos = CHARINDEX(#DelimChar, #Chunk, #ChunkPos)
END
-- adjust positions
SET #TextPos = #TextPos + #ChunkLen
END
-- handle last item
IF #ItemBegPos <= #InputLen
BEGIN
-- insert position
INSERT INTO #list_items (item_order, item_begpos, item_endpos)
VALUES (#ItemOrder, #ItemBegPos, #InputLen)
END
-- delete the bad items
DELETE FROM #list_items
WHERE item_endpos < item_begpos
-- return list items
SELECT SUBSTRING(#list_text, item_begpos, (item_endpos - item_begpos + 1)) AS item_text, item_order, item_begpos, item_endpos
FROM #list_items
ORDER BY item_order
END
DROP TABLE #list_items
RETURN
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
I have same idea with user KM. but do not need extra table Number. Just this function only.
CREATE FUNCTION [dbo].[FN_ListToTable]
(
#SplitOn char(1) --REQUIRED, the character to split the #List string on
,#List varchar(8000) --REQUIRED, the list to split apart
)
RETURNS
#ParsedList table
(
ListValue varchar(500)
)
AS
BEGIN
DECLARE #number int = 0
DECLARE #childString varchar(502) = ''
DECLARE #lengthChildString int = 0
DECLARE #processString varchar(502) = #SplitOn + #List + #SplitOn
WHILE #number < LEN(#processString)
BEGIN
SET #number = #number + 1
SET #lengthChildString = CHARINDEX(#SplitOn, #processString, #number + 1) - #number - 1
IF #lengthChildString > 0
BEGIN
SET #childString = LTRIM(RTRIM(SUBSTRING(#processString, #number + 1, #lengthChildString)))
IF #childString IS NOT NULL AND #childString != ''
BEGIN
INSERT INTO #ParsedList(ListValue) VALUES (#childString)
SET #number = #number + #lengthChildString - 1
END
END
END
RETURN
END
And here is the test:
SELECT ListValue FROM dbo.FN_ListToTable('/','a/////bb/c')
Result:
ListValue
______________________
a
bb
c
-- select * from dbo.Split_ID('77,106')
ALTER FUNCTION dbo.Split_ID(#String varchar(8000))
returns #temptable TABLE (ID varchar(8000))
as
begin
declare #idx int
declare #slice varchar(8000)
declare #Delimiter char(1)
set #Delimiter =','
select #idx = 1
if len(#String)<1 or #String is null return
while #idx!= 0
begin
set #idx = charindex(#Delimiter,#String)
if #idx!=0
set #slice = left(#String,#idx - 1)
else
set #slice = #String
if(len(#slice)>0)
insert into #temptable(ID) values(#slice)
set #String = right(#String,len(#String) - #idx)
if len(#String) = 0 break
end
return
end
You could do it like this:
create or replace
PROCEDURE UDP_SETBOOKMARK
(
P_USERID IN VARCHAR2
, P_BOOKMARK IN VARCHAR2
) AS
BEGIN
UPDATE T_ER_Bewertung
SET LESEZEICHEN = P_BOOKMARK
WHERE STAMM_ID in( select regexp_substr(P_USERID,'[^,]+', 1, level) from dual
connect by regexp_substr(P_USERID, '[^,]+', 1, level) is not null )
and ER_ID = (select max(ER_ID) from T_ER_Bewertung_Kopie);
commit;
END UDP_SETBOOKMARK;
Then try it with
Begin
UDP_SETBOOKMARK ('1,2,3,4,5', 'Test');
End;
You can use this IN-Clause with regexp_substr in other situations too, just try it.
Error 493: The column 'i' that was returned from the nodes() method cannot be
used directly. It can only be used with one of the four XML data type
methods, exist(), nodes(), query(), and value(), or in IS NULL and IS NOT
NULL checks.
The above errorr was fixed in SQL Server 2014 by using following snippet
Declare #Ids varchar(50)
Set #Ids = '1,2,3,5,4,6,7,98,234'
DECLARE #XML XML
SET #XML = CAST('<i>' + REPLACE(#Ids, ',', '</i><i>') + '</i>' AS XML)
SELECT SomeTable.*
FROM
SomeTable
cross apply #XML.nodes('i') x(i)
where SomeTable .Id = x.i.value('.', 'VARCHAR(MAX)')
Try this:
SELECT ProductId, Name, Tags
FROM Product
WHERE '1,2,3,' LIKE '%' + CAST(ProductId AS VARCHAR(20)) + ',%';
As said on the last example of this link
Tons of answers here, but to add my two cents I think STRING_SPLIT is a very simple approach to this sort of problem:
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN;
(SELECT value FROM STRING_SPLIT(#Ids, ','))
Best and simple approach.
DECLARE #AccumulateKeywordCopy NVARCHAR(2000),#IDDupCopy NVARCHAR(50);
SET #AccumulateKeywordCopy ='';
SET #IDDupCopy ='';
SET #IDDup = (SELECT CONVERT(VARCHAR(MAX), <columnName>) FROM <tableName> WHERE <clause>)
SET #AccumulateKeywordCopy = ','+#AccumulateKeyword+',';
SET #IDDupCopy = ','+#IDDup +',';
SET #IDDupCheck = CHARINDEX(#IDDupCopy,#AccumulateKeywordCopy)
CREATE TABLE t
(
id INT,
col1 VARCHAR(50)
)
INSERT INTO t
VALUES (1,
'param1')
INSERT INTO t
VALUES (2,
'param2')
INSERT INTO t
VALUES (3,
'param3')
INSERT INTO t
VALUES (4,
'param4')
INSERT INTO t
VALUES (5,
'param5')
DECLARE #params VARCHAR(100)
SET #params = ',param1,param2,param3,'
SELECT *
FROM t
WHERE Charindex(',' + Cast(col1 AS VARCHAR(8000)) + ',', #params) > 0
working fiddle find here Fiddle
I ran into the same issue, and I don't want to have any footprint on the source database - i.e. no stored procedures or functions. I went about it this way:
declare #IDs table (Value int)
insert into #IDs values(1)
insert into #IDs values(2)
insert into #IDs values(3)
insert into #IDs values(5)
insert into #IDs values(4)
insert into #IDs values(6)
insert into #IDs values(7)
insert into #IDs values(98)
insert into #IDs values(234)
SELECT *
FROM sometable
WHERE tableid IN (select Value from #IDs)
The answer by #RBarryYoung (above) worked for me.
But when you have spaces in between the comma separated string values, then it will omit IDs with spaces. So I removed the spaces.
Take a look at the code snippet below.
Declare #Ids varchar(50) Set #Ids = '1 , 2,3'
set #Ids=','+Replace(#Ids,' ', '')+',';
Select * from [tblEmployee]
where Charindex(','+cast(ID as varchar(8000))+',', #Ids) > 0
WHERE someId IN (SELECT convert(int, value) FROM string_split(#stringOfCommaDelimitedIds, ','))
The simplest way i found was to use FIND_IN_SET
FIND_IN_SET(column_name, values)
values=(1,2,3)
SELECT name WHERE FIND_IN_SET(id, values)
This came in handy for one of my requirements where I did not want to use CTE and also did not want to go with the inner join.
DECLARE #Ids varchar(50);
SET #Ids = '1,2,3,5,4,6,7,98,234';
SELECT
cn1,cn2,cn3
FROM tableName
WHERE columnName in (select Value from fn_SplitString(#ids, ','))
Function for split string :
CREATE FUNCTION [dbo].[fn_SplitString] ( #stringToSplit VARCHAR(MAX), #seperator Char )
RETURNS
#returnList TABLE ([Value] [nvarchar] (500))
AS
BEGIN
DECLARE #name NVARCHAR(255)
DECLARE #pos INT
WHILE CHARINDEX(#seperator, #stringToSplit) > 0
BEGIN
SELECT #pos = CHARINDEX(#seperator, #stringToSplit)
SELECT #name = SUBSTRING(#stringToSplit, 1, #pos-1)
INSERT INTO #returnList
SELECT #name
SELECT #stringToSplit = SUBSTRING(#stringToSplit, #pos+1, LEN(#stringToSplit)-#pos)
END
INSERT INTO #returnList
SELECT #stringToSplit
RETURN
END