How to split string on two characters or delimiters in sql - sql

i'm trying to split this string "1,1_5,2_3,4" first on '_' and then split the sub string on ',' so it will be like "1,1_5,2_3,4" split-ed on '_' first so it will give this sub string "1,1" then this sub string split-ed on ',' and then insert the split-ed sub string "1 1" into temp table like this
INSERT INTO [dbo].[TEST] ([X],[Y])
VALUES (#X,#Y)
i did this with only split on one delimiter ',' like this
first i create a table Split function
ALTER FUNCTION [dbo].[Split]
(
-- Add the parameters for the function here
#RowData nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
-- Declare the return variable here
Declare #Cnt int
Set #cnt = 1
While(charindex(#SplitOn,#RowData) > 0)
Begin
Insert Into #RtnValue (data)
Select
Data = ltrim(RTRIM(SUBSTRING(#RowData,1,CHARINDEX(#SplitOn,#RowData)-1)))
set #RowData = SUBSTRING(#RowData,CHARINDEX(#SplitOn,#RowData)+1,len(#RowData))
set #Cnt = #Cnt + 1
End
insert into #RtnValue(data)
select Data = ltrim(RTRIM(#RowData))
RETURN
END
then i create this stored procedure
ALTER PROCEDURE [dbo].[uspTest]
-- Add the parameters for the stored procedure here
#StringOFXIDs nvarchar(2000)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE #SOMETABLE TABLE(ID INT IDENTITY(1,1) UNIQUE, XID INT);
DECLARE #XCOUNT INT;
DECLARE #XCURRENT INT;
DECLARE #XCOUNTER INT = 1;
-- Insert statements for procedure here
INSERT INTO #SOMETABLE([XID])
SELECT [Data] FROM [dbo].[Split](#StringOFXIDs,',');
SELECT #XCOUNT = COUNT(1) FROM #SOMETABLE;
WHILE (#XCOUNTER <= #XCOUNT)
BEGIN
SELECT #XCOUNTER = [XID]
FROM #SOMETABLE
WHERE [ID] = #XCOUNTER
INSERT INTO [dbo].[TEST] ([X])
VALUES (#XCOUNTER)
SELECT #XCOUNTER +=1;
END
END
--EXEC [dbo].[uspTest] '1,2,3,4'
and then execute this stored procedure and every thing work but i can't figure out how to split the string on two characters or delimiters and then inserted to temp table thanks for any help in advance.

You can use XML functionality to split into rows, then split into columns:
DECLARE #string VARCHAR(100) = '1,1_5,2_3,4'
;WITH cte AS (SELECT RTRIM(LTRIM(Split.a.value('.', 'VARCHAR(100)'))) AS Txt
,ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RN
FROM (SELECT CAST ('<M>' + REPLACE(#string, '_', '</M><M>') + '</M>' AS XML) AS DATA
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
)
,cte2 AS (SELECT CONVERT(XML,'<String><Section>'+ REPLACE(REPLACE(Txt ,'|',','),',', '</Section><Section>') + '</Section></String>') AS Txt
,RN
FROM cte
)
SELECT Txt.value('/String[1]/Section[1]','varchar(100)') AS Col1
,Txt.value('/String[1]/Section[2]','varchar(100)') AS Col2
FROM cte2
ORDER BY RN
Outputs:
Col1 Col2
---------------
1 1
5 2
3 4

Related

insert multiple splited string column to separate rows into a sql table

I have a table as shown below
Is it possible to insert the above table data into a table in separate rows?
I tried using split function on each column and stored each column result on a temp table. I have no clue how to insert into new table combining all these rows and columns as per the id. Any help or suggestion would help.
Try this answer. Hope this helps you.
DECLARE #Table TABLE(ID INT, NAME VARCHAR(10),TITLE VARCHAR(10))
INSERT INTO #Table VALUES (1,';a;b;c',';12;13;14')
DECLARE #ID INT=1
SELECT #ID ID,Items,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) RN1 INTO #T1 FROM dbo.split((SELECT NAME FROM #Table WHERE id=#ID),';')
SELECT #ID ID,Items,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) RN2 INTO #T2 FROM dbo.split((SELECT TITLE FROM #Table WHERE id=#ID),';')
SELECT T1.ID,T1.Items NAME,T2.Items TITLE
FROM #T1 T1 INNER JOIN #T2 T2 ON T1.RN1=T2.RN2
DROP TABLE #T1
DROP TABLE #T2
If you want all the values, you just try the looping method like WHILE.
DECLARE #Table TABLE(ID INT, NAME VARCHAR(10),TITLE VARCHAR(10))
INSERT INTO #Table VALUES (1,';a;b;c',';12;13;14'),(2,';c;f;u',';67;56;34'),(3,';l;k;m',';90;70;60')
DECLARE #MinID INT,#MaxID INT
SELECT #MinID=MIN(ID),#MaxID=MAX(ID) FROM #Table
CREATE TABLE #T1(ID INT,Items VARCHAR(10),RN1 INT)
CREATE TABLE #T2(ID INT,Items VARCHAR(10),RN2 INT)
WHILE #MinID<=#MaxID
BEGIN
INSERT INTO #T1
SELECT #MinID ID,Items,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) RN1
FROM dbo.split((SELECT NAME FROM #Table WHERE id=#MinID),';')
INSERT INTO #T2
SELECT #MinID ID,Items,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) RN2
FROM dbo.split((SELECT TITLE FROM #Table WHERE id=#MinID),';')
SET #MinID=#MinID+1
END
SELECT T1.ID,T1.Items NAME,T2.Items TITLE
FROM #T1 T1 INNER JOIN #T2 T2 ON T1.ID=T2.ID AND T1.RN1=T2.RN2
DROP TABLE #T1
DROP TABLE #T2
This will produce the result, what you exactly want:
ID NAME TITLE
----------- ---------- ----------
1 a 12
1 b 13
1 c 14
2 c 67
2 f 56
2 u 34
3 l 90
3 k 70
3 m 60
Here is the split function, I used to split the Strings:
CREATE FUNCTION [dbo].[Split]
(#String VARCHAR (max), #Delimiter CHAR (1))
RETURNS
#temptable TABLE (
[items] VARCHAR (max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL)
AS
begin
declare #idx int
declare #slice varchar(max)
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 is another method of CTE with help of XML node
There will no need to create any function.
WITH cte AS (
SELECT ID,
split.a.value('.', 'NVARCHAR(MAX)') [name],
ROW_NUMBER() OVER(ORDER BY ( SELECT 1)) RN
FROM
(
SELECT ID,
CAST('<A>'+REPLACE(name, ';', '</A><A>')+'</A>' AS XML) AS [name]
FROM <table_name>
) a
CROSS APPLY name.nodes('/A') AS split(a)),
CTE1 AS (
SELECT ID,
split.a.value('.', 'NVARCHAR(MAX)') [title],
ROW_NUMBER() OVER(ORDER BY ( SELECT 1 )) RN
FROM
(
SELECT ID,
CAST('<A>'+REPLACE(title, ';', '</A><A>')+'</A>' AS XML) AS [title]
FROM <table_name>
) aa
CROSS APPLY title.nodes('/A') AS split(a))
SELECT C.ID, C.name, C1.title FROM CTE C
JOIN CTE1 C1 ON C1.RN = C.RN
WHERE C.name != '' AND C1.title != '';
Result :
ID name title
1 a 12
1 b 13
1 s 45
2 c 67
2 f 56
2 u 34
3 l 90
3 k 70
3 m 60
Try below way .. this will save time and memory also!
This T-SQL block has dependency on dbo.SplitString function ..
T1 is my Source table T2 is my Destination table
DECLARE #c_s AS CURSOR;
DECLARE #id INT;
DECLARE #name VARCHAR(1000);
DECLARE #value VARCHAR(1000);
SET #c_s = CURSOR FOR SELECT * FROM T1;
OPEN #c_s;
FETCH #c_s INTO #id, #name, #value
WHILE ##FETCH_STATUS = 0
BEGIN
INSERT INTO T2
SELECT #id
, a.Value NAME
, b.value value
FROM dbo.SplitString(#name, ';') a
INNER JOIN dbo.SplitString(#value, ';') b
ON a.OrdinalPosition = b.OrdinalPosition
FETCH NEXT FROM #c_s INTO #id, #name, #value
END
here is the dbo.SplitString
CREATE FUNCTION [dbo].[SplitString](#givenString VARCHAR(8000) , #separator VARCHAR(100))
RETURNS TABLE AS
RETURN (
WITH data([start], [end]) AS (
SELECT 0 AS [start]
, CHARINDEX(#separator, #givenString) AS [end]
UNION ALL
SELECT [end] + 1
, CHARINDEX(#separator, #givenString, [end] + 1)
FROM data
WHERE [end] > 0
)
SELECT ROW_NUMBER() OVER (
ORDER BY OrdinalPosition
) OrdinalPosition
, RTRIM(LTRIM(Value)) Value
FROM (
SELECT ROW_NUMBER() OVER (
ORDER BY [start]
) OrdinalPosition
, SUBSTRING(#givenString, [start], COALESCE(NULLIF([end], 0), len(#givenString) + 1) - [start]) Value
FROM data
) r
WHERE RTRIM(Value) <> ''
AND Value IS NOT NULL
)
You can achieve this by writing a table-valued function that will split the strings according to your requirements. Once you have created this object, then you can use the T-SQL in second code snippet to get your final required table.
The definition of this table-valued function is as given below. Just copy and paste this into SSMS and run it against your database.
Split a string function
-- =============================================
-- Author: B Vidhya
-- Create date: Nov 7, 2017
-- Description: Splits a string and returns a table
-- =============================================
CREATE FUNCTION [dbo].[SplitAString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #splitTable TABLE (
ItemNumber INT IDENTITY(1,1),
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE #startIndex INT,#endIndex INT
SET #startIndex = 1
IF SUBSTRING(#string, LEN(#string) - 1, LEN(#string)) <> #delimiter
BEGIN
SET #string = #string + #delimiter
END
WHILE CHARINDEX(#delimiter, #string) > 0
BEGIN
SET #endIndex = CHARINDEX(#delimiter, #string)
INSERT INTO #splitTable(Item)
SELECT SUBSTRING(#string, #startIndex, #endIndex - 1)
SET #string = SUBSTRING(#string, #endIndex + 1, LEN(#string))
END
RETURN
END
GO
In T-SQL below, I have called the original table StackOverflowTable1 and you can replace this table name with your actual table name. Also, I am inserting final rows into a table variable. If you wanted to insert into your custom table, then you could use perform an INSERT into your table after the END of WHILE loop.
T-SQL to get your final table
DECLARE #myTable TABLE
(Id INT,
Name VARCHAR(5000),
Title VARCHAR(5000)
);
DECLARE #lastId INT= 0, #id INT, #name VARCHAR(5000), #title VARCHAR(5000);
--for each record in table perform splitting and insertion in new table
WHILE EXISTS
(
SELECT 1
FROM StackOverFlowTable1 soft
WHERE Id > #lastId
)
BEGIN
SELECT TOP (1) #id = Id,
#name = Name,
#title = Title
FROM StackOverFlowTable1 soft
WHERE Id > #lastId
ORDER BY Id;
SET #lastId = #id;
INSERT INTO #myTable
(Id,
Name,
Title
)
SELECT #id,
ss1.Item,
ss2.Item
FROM dbo.SplitString(#name, ';') ss1
INNER JOIN dbo.SplitString(#title, ';') ss2 ON ss1.ItemNumber = ss2.ItemNumber
WHERE ss1.Item <> ''
AND ss2.Item <> '';
END;
SELECT * FROM #myTable;
I do not agree with Dinesh script because it is based on RBAR.
I have very similar Split function with also return row_number along with item.
so test my script along with other sample data.
DECLARE #Table TABLE(ID INT, NAME VARCHAR(10),TITLE VARCHAR(10))
INSERT INTO #Table VALUES (1,',a,b,c',',12,13,14')
SELECT id
,t.RowVal
,a.RowVal
FROM (
SELECT t.id
,a.RowNum
,a.RowVal
,t.TITLE
FROM #Table t
CROSS APPLY (
SELECT *
FROM dbo.FN_SPLIT_VALUE(t.NAME)
) a
) t
CROSS APPLY (
SELECT *
FROM dbo.FN_SPLIT_VALUE(t.TITLE)
WHERE t.RowNum = RowNum
) a
WHERE t.RowVal <> ''

How to convert comma separated NVARCHAR to table records in SQL Server 2005?

I have a list of ids separated by comma like:
1,17,25,44,46,67,88
I want to convert them to a table records ( into a temporary table ) like
#tempTable
number_
--------
1
17
25
44
46
67
88
It is possible with a function, a table-valued one ?
Why I want this ? I want to use for INNER JOIN clause (into stored procedure) with another table(s) like as:
SELECT a,b,c FROM T1
INNER JOIN functionNameWhichReturnsTable
ON functionNameWhichReturnsTable.number_ = T1.a
I cannot use IN because I will use stored procedure which accepts a parameter of type NVARCHAR. That parameter will provide the list of ids.
Thank you
Possible duplicate of separate comma separated values and store in table in sql server.
Please try a precise one from Comma-Delimited Value to Table:
CREATE FUNCTION [dbo].[ufn_CSVToTable] ( #StringInput VARCHAR(8000), #Delimiter nvarchar(1))
RETURNS #OutputTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN
DECLARE #String VARCHAR(10)
WHILE LEN(#StringInput) > 0
BEGIN
SET #String = LEFT(#StringInput,
ISNULL(NULLIF(CHARINDEX(#Delimiter, #StringInput) - 1, -1),
LEN(#StringInput)))
SET #StringInput = SUBSTRING(#StringInput,
ISNULL(NULLIF(CHARINDEX(#Delimiter, #StringInput), 0),
LEN(#StringInput)) + 1, LEN(#StringInput))
INSERT INTO #OutputTable ( [String] )
VALUES ( #String )
END
RETURN
END
GO
Check the requirement in other way using XML:
DECLARE #param NVARCHAR(MAX)
SET #param = '1:0,2:1,3:1,4:0'
SELECT
Split.a.value('.', 'VARCHAR(100)') AS CVS
FROM
(
SELECT CAST ('<M>' + REPLACE(#param, ',', '</M><M>') + '</M>' AS XML) AS CVS
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)
Here's a trick that doesn't need a function or XML.
Basically the string gets transformed into a single insert statement for a temporary table.
The temp table can then be used for further processing.
IF OBJECT_ID('tempdb..#tmpNum') IS NOT NULL
DROP TABLE #tmpNum;
CREATE TABLE #tmpNum (num int);
DECLARE #TEXT varchar(max) = '1,17,25,44,46,67,88';
DECLARE #InsertStatement varchar(max);
SET #InsertStatement = 'insert into #tmpNum (num) values ('+REPLACE(#TEXT,',','),(')+');';
EXEC (#InsertStatement);
-- use the temp table
SELECT *
FROM YourTable t
WHERE t.id IN (SELECT DISTINCT num FROM #tmpNum);
This method is usable for up to 1000 values.
Because 1000 is the max limit of a row value expression.
Also, as Stuart Ainsworth pointed out.
Since this method uses Dynamic Sql, be wary of code injection and don't use it for strings based on user input.
Side-note
Starting from MS Sql Server 2016, one could simply use the STRING_SPLIT function.
DECLARE #TEXT varchar(max);
SET #TEXT = '1,17,25,44,46,67,88';
SELECT t.*
FROM YourTable t
JOIN (SELECT DISTINCT CAST(value AS INT) num FROM STRING_SPLIT(#TEXT, ',')) nums
ON t.id = nums.num;
Completing the answers, you could also use the CSV string to store multiple values in multiple columns:
--input sql text
declare #text_IN varchar(max) ='text1, text1.2, text1.3, 1, 2010-01-01\r\n text2, text2.2, text2.3, 2, 2016-01-01'
Split the csv file into rows:
declare #temptable table (csvRow varchar(max))
declare #DelimiterInit varchar(4) = '\r\n'
declare #Delimiter varchar(1) = '|'
declare #idx int
declare #slice varchar(max)
set #text_IN = REPLACE(#text_IN,#DelimiterInit,#Delimiter)
select #idx = 1
if len(#text_IN)<1 or #text_IN is null return
while #idx!= 0
begin
set #idx = charindex(#Delimiter,#text_IN)
if #idx!=0
set #slice = left(#text_IN,#idx - 1)
else
set #slice = #text_IN
if(len(#slice)>0)
insert into #temptable(csvRow) values(#slice)
set #text_IN = right(#text_IN,len(#text_IN) - #idx)
if len(#text_IN) = 0 break
end
Split rows into columns:
;WITH XMLTable (xmlTag)
AS
(
SELECT CONVERT(XML,'<CSV><champ>' + REPLACE(csvRow,',', '</champ><champ>') + '</champ></CSV>') AS xmlTag
FROM #temptable
)
SELECT RTRIM(LTRIM(xmlTag.value('/CSV[1]/champ[1]','varchar(max)'))) AS Column1,
RTRIM(LTRIM(xmlTag.value('/CSV[1]/champ[2]','varchar(max)'))) AS Column2,
RTRIM(LTRIM(xmlTag.value('/CSV[1]/champ[3]','varchar(max)'))) AS Column3,
RTRIM(LTRIM(xmlTag.value('/CSV[1]/champ[4]','int'))) AS Column4,
RTRIM(LTRIM(xmlTag.value('/CSV[1]/champ[5]','datetime'))) AS Column5
FROM XMLTable
The following works:
declare #parStoreNo As varchar(8000) = '1,2,3,4'
CREATE TABLE #parStoreNo (StoreNo INT)-- drop #parStoreNo
declare #temptable VARCHAR(1000) = #parStoreNo
declare #SQL VARCHAR(1000)
SELECT #SQL = CONVERT(VARCHAR(1000),' select ' + REPLACE(ISNULL(#temptable,' NULL '),',', ' AS Col UNION ALL SELECT '))
INSERT #parStoreNo (StoreNo)
EXEC (#SQL)
I am using XML Function as below...
DECLARE #str VARCHAR(4000) = '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832,833,836,837,846,913,914,919,922,923,924,925,926,927,927,928,929,929,930,931,932,934,935,1029,1072,1187,1188,1192,1196,1197,1199,1199,1199,1199,1200,1201,1202,1203,1204,1205,1206,1207,1208,1209,1366,1367,1387,1388,1666,1759,1870,2042,2045,2163,2261,2374,2445,2550,2676,2879,2880,2881,2892,2893,2894'
Declare #x XML
select #x = cast('<A>'+ replace(#str,',','</A><A>')+ '</A>' as xml)
select t.value('.', 'int') as inVal
from #x.nodes('/A') as x(t)
I prefer this because not need to create any separate function and proc. Also I don't have to opt dynamic SQL query which I prefer most.
Convert Comma Separated String to Table
DECLARE #str VARCHAR(4000) = '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832'
DECLARE #x XML
select #x = cast('<A>'+ replace(#str,',','</A><A>')+ '</A>' as xml)
select t.value('.', 'int') as inVal
from #x.nodes('/A') as x(t)
Try this code
SELECT RTRIM(part) as part
INTO Table_Name
FROM dbo.splitstring(#Your_Comma_string,',')
splitstring Function is as follows
CREATE FUNCTION dbo.splitstring ( #stringToSplit VARCHAR(MAX) )
RETURNS
#returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN
DECLARE #name NVARCHAR(255)
DECLARE #pos INT
WHILE CHARINDEX(',', #stringToSplit) > 0
BEGIN
SELECT #pos = CHARINDEX(',', #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

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.

Insert multiple rows into temp table with one command in SQL2005

I've got some data in the following format:
-1,-1,-1,-1,701,-1,-1,-1,-1,-1,304,390,403,435,438,439,442,455
I need to insert it into a temp table like this:
CREATE TABLE #TEMP
(
Node int
)
So that I can use it in a comparison with data in another table.
The data above represents separate rows of the "Node" column.
Is there an easy way to insert this data, all in one command?
Also, the data will actually being coming in as seen, as a string... so I need to be able to just concat it into the SQL query string. I can obviously modify it first if needed.
Try something like
CREATE TABLE #TEMP
(
Node int
)
DECLARE #textXML XML
DECLARE #data NVARCHAR(MAX),
#delimiter NVARCHAR(5)
SELECT #data = '-1,-1,-1,-1,701,-1,-1,-1,-1,-1,304,390,403,435,438,439,442,455 ',
#delimiter = ','
SELECT #textXML = CAST('<d>' + REPLACE(#data, #delimiter, '</d><d>') + '</d>' AS XML)
INSERT INTO #TEMP
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM #textXML.nodes('/d') T(split)
SELECT * FROM #TEMP
DROP TABLE #TEMP
You can create a query dynamically like this:
declare #sql varchar(1000)
set #sql = 'insert into #TEMP select ' + replace(#values, ',', ' union all select ')
exec #sql
As always when creating queries dynamically, you have to be careful so that you only use trusted data.
I would create a function that would return a table variable and then join that function into the select
Use:
select * from myTable a
inner join dbo.buildTableFromCSV('1,2,3') on a.id = b.theData
Here is my function for doing this
CREATE FUNCTION [dbo].[buildTableFromCSV] ( #csvString varchar(8000) ) RETURNS #myTable TABLE (ID int identity (1,1), theData varchar(100))
AS BEGIN
DECLARE #startPos Int -- position to chop next block of chars from
DECLARE #currentPos Int -- position to current character we're examining
DECLARE #strLen Int
DECLARE #c char(1) -- current subString
-- variable initalization
-- -------------------------------------------------------------------------------------------------------------------------------------------------
SELECT #csvString = #csvString + ','
SELECT #startPos = 1
SELECT #currentPos = 1
SELECT #strLen = Len(#csvString)
-- loop over string and build temp table
-- -------------------------------------------------------------------------------------------------------------------------------------------------
WHILE #currentPos <= #strLen BEGIN
SET #c = SUBSTRING(#csvString, #currentPos, 1 )
IF ( #c = ',' ) BEGIN
IF ( #currentPos - #startPos > 0 ) BEGIN
INSERT
INTO #myTable ( theData )
VALUES ( CAST( SUBSTRING ( #csvString, #startPos, #currentPos - #startPos) AS varchar ) )
END
ELSE
begin
INSERT
INTO #myTable ( theData )
VALUES ( null )
end
SELECT #startPos = #currentPos + 1
END
SET #currentPos = #currentPos + 1
END
delete from #myTable where theData is null
return
END

T-SQL script or function that will get selected values from a comma delimited string

If I have a string with values like A,B,C,D,E,F, and I wanted to remove the last comma, is there a function or script I can use to do that?
The below example will trim off the last char of a string but wont care if it's a comma or not
DECLARE #s VARCHAR(50)
SET #s = 'a,b,'
SELECT LEFT(#s, LEN(#s)-1)
If I'm understanding you additional question correctly, I believe you want to archive something like the below...
DECLARE #b TABLE
(
[stuff] VARCHAR(2)
)
INSERT INTO #b VALUES('c')
DECLARE #s VARCHAR(MAX)
SET #s = 'a,b,c'
SELECT [stuff]
FROM #b b
INNER JOIN dbo.list(#s) lst
ON lst.val = b.[stuff]
CREATE function [dbo].[list] ( #list VARCHAR(MAX) )
RETURNS #list_table TABLE ([val] VARCHAR(20))
AS
BEGIN
DECLARE #index INT,
#start_index INT,
#val VARCHAR(20)
SELECT #index = 1
SELECT #start_index = 1
WHILE #index <= DATALENGTH(#list)
BEGIN
IF SUBSTRING(#list,#index,1) = ','
BEGIN
SELECT #val = SUBSTRING(#list, #start_index, #index - #start_index )
INSERT #list_table ([val]) VALUES (#val)
SELECT #start_index = #index + 1
END
SELECT #index = #index + 1
END
SELECT #val = SUBSTRING(#list, #start_index, #index - #start_index )
INSERT #list_table ([val]) VALUES (#val)
RETURN
END
The function just splits up the input string into rows in a table with a column "val" - this means you can then pass in a comma delimited string and use it to filter another table.