Hot to convert a variable with value '1,2,3' to a table (every number as a record) - sql

Working on SQL (2005 and 2008)
the variable with value '1,2,3' would be call #cedis and this could to have N number for example
set #cedis='1' or set #cedis='1,2,3,4,5,6,7' or set #cedis='125,98,91'
so important, its this must to be a select only, a loop could not to be use, only a select!
this must to return a (result as ) table with values for example
set #cedis='1,2,3,4' this must to return a result
number 1 2 3 4
declare #cedis varchar(max)
set #cedis='1,58,123,8'
;with datos as
(
my select with is going to return me the table
)
select * from datos
result set is
number
1
58
123
8

If am not wrong this is what you need
DECLARE #cedis VARCHAR(500)='1,2,3,4'
SELECT Split.a.value('.', 'VARCHAR(100)') Numbers
FROM (SELECT Cast ('<M>' + Replace(#cedis, ',', '</M><M>') + '</M>' AS XML) AS Numbers) AS A
CROSS APPLY Numbers.nodes ('/M') AS Split(a)
Result:
Numbers
-------
1
2
3
4

A table valued function would do it.
CREATE FUNCTION [dbo].[fn_Split](#text VARCHAR(MAX), #delimiter VARCHAR(5) = ',')
RETURNS #Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value VARCHAR(8000)
)
AS
BEGIN
DECLARE #index int
SET #index = -1
WHILE (LEN(#text) > 0)
BEGIN
SET #index = CHARINDEX(#delimiter , #text)
IF (#index = 0) AND (LEN(#text) > 0)
BEGIN
INSERT INTO #Strings VALUES (#text)
BREAK
END
IF (#index > 1)
BEGIN
INSERT INTO #Strings VALUES (LEFT(#text, #index - 1))
END
SET #text = RIGHT(#text, (LEN(#text) - (#index+LEN(#delimiter)-1)))
END
RETURN
END
You can call it as follows:
SELECT *
FROM dbo.fn_Split(#cedis,',')

Here is a more generic solution that breaks any given string into a table based on any given separator:
http://rextester.com/VSRDLS48817
Not an original idea, but I've found it very useful.
create function [dbo].[SplitString]
(
#str nvarchar(255),
#separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
select
cast(1 as int),
cast(1 as int),
charindex(#separator, #str)
union all
select
p + 1,
b + 1,
charindex(#separator, #str, b + 1)
from tokens
where b > 0
)
select
p-1 ItemIndex,
substring(
#str,
a,
case when b > 0 then b-a ELSE LEN(#str) end)
AS Item
from tokens
);

This is another one approach to get required output result
DECLARE #cedis VARCHAR(MAX) ,
#delimeter VARCHAR(10)
SET #cedis = '1,58,123,8,14144,15,155231,15,3647,2347,45,76,68,2354,577,5'
SET #delimeter = ','
SET #cedis = #cedis + #delimeter;
WITH datos
AS ( SELECT n = 1
UNION ALL
SELECT n + 1
FROM datos
WHERE n <= LEN(#cedis)
),
cte
AS ( SELECT T.N ,
ROW_NUMBER() OVER ( ORDER BY T.N ) AS RN
FROM datos AS T
WHERE SUBSTRING(#cedis, T.N, LEN(#delimeter)) = #delimeter
AND LEN(#cedis) >= T.N
)
SELECT SUBSTRING(#cedis, COALESCE(R.N + LEN(#delimeter), 1),
L.N - COALESCE(R.N + LEN(#delimeter), 1)) AS part ,
L.RN AS ID
FROM cte AS L
LEFT JOIN cte AS R ON L.RN = R.RN + 1
OPTION ( MAXRECURSION 1000 )

Related

For loop in SQL to find 1's position in a sequence of 0's and 1's

Below is the for loop in c# which returns 1's position in a sequence of 0's and 1's here is my code
public string OnesPosition(string statusBits)
{
string onePos = "";
for (int i = 0; i < statusBits.Length; i++)
{
if (statusBits[i] == '1')
{
onePos = onePos + Convert.ToSingle(i + 1) + ",";
}
}
onePos = string.IsNullOrEmpty(onePos ) ? "0," : onePos ;
return onePos;
}
var result = OnesPosition("00000000000101");
This will return: result = 12,14
How I can do this in sql query or using SQL function?
Using SQL Server Management Studio v17.9
Just another option
Example
Declare #S varchar(50) = '00000000000101'
Select Stuff((Select concat(',',N )
From (
Select Top (len(#S)) N=Row_Number() Over (Order By (Select NULL))
From master..spt_values
) s
Where substring(#S,N,1)='1'
Order By N
For XML Path ('')),1,1,'')
Returns
12,14
Loops don't play well in SQL Server, but here is a method.
declare #ones varchar(16) = '00000000100101'
declare #pos int = 1
declare #result varchar(256) = ''
while #pos <= len(#ones)
begin
set #result = #result + case when substring(#ones,#pos,1) = 1 then ',' + cast(#pos as varchar) else '' end
set #pos = #pos + 1
end
select right(#result,len(#result) - 1)
Solution:
Another possible approach is using recursive CTE (which returns each digit and digit positon) and group concatenation.
Using STRING_AGG() (from SQL Server 2017):
DECLARE #ones varchar(16)
SET #ones = '1000000001001011';
WITH Digits AS (
SELECT 1 AS DigitPosition, SUBSTRING(#ones, 1, 1) AS Digit
UNION ALL
SELECT DigitPosition + 1, SUBSTRING(#ones, DigitPosition + 1, 1)
FROM Digits
WHERE DigitPosition < LEN(#ones)
)
SELECT STRING_AGG(DigitPosition, ',')
FROM Digits
WHERE Digit = '1'
Using FOR XML:
DECLARE #ones varchar(16)
SET #ones = '1000000001001011';
WITH Digits AS (
SELECT 1 AS DigitPosition, SUBSTRING(#ones, 1, 1) AS Digit
UNION ALL
SELECT DigitPosition + 1, SUBSTRING(#ones, DigitPosition + 1, 1)
FROM Digits
WHERE DigitPosition < LEN(#ones)
)
SELECT CONVERT(varchar(max), DigitPosition) + ','
FROM Digits
WHERE Digit = '1'
FOR XML PATH('')
Output:
1,10,13,15,16
One more method using a tally table and just straight tsql:
WITH Tally(i) AS (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS i
FROM (VALUES (0), (0), (0), (0), (0), (0), (0)) a(n)
CROSS JOIN (VALUES (0), (0), (0), (0), (0), (0)) b(n)
)
SELECT bitloc
FROM
(
SELECT SUBSTRING(x.d, i, 1) as bitset, i bitloc
FROM (VALUES ('00000000000101')) x(d)
CROSS JOIN Tally
) sub
WHERE bitset = 1
Here is function based on #scsimon response and it worked.
CREATE FUNCTION [dbo].[ConvertTo1Positions]
(
#ones AS varchar(16),
#pos AS INT = 1,
#result varchar(256)
) RETURNS VARCHAR(MAX) AS BEGIN
while #pos <= len(#ones)
begin
set #result = #result + iif(substring(#ones,#pos,1) = 1,cast(#pos as char),'')
set #pos = #pos + 1
end
RETURN #result;
END
I don't think it's a good idea to use loop, but since you want it
DECLARE #Str VARCHAR(45) = '000001000000101',
#OutPut VARCHAR(45) = '',
#I INT = 1;
WHILE #I <= LEN(#Str)
BEGIN
IF (SELECT SUBSTRING(#Str, #I, 1)) = '1'
SET #OutPut = #OutPut + (SELECT CAST(#I AS VARCHAR(10))) + ',';
--Convert.ToSingle(i + 1) why +1?
SET #I = #I + 1;
END
SELECT #OutPut;
or this...
DECLARE #string VARCHAR(100) = '00000000000101';
WITH
cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),
cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),
cte_Tally (n) AS (
SELECT TOP (LEN(#string))
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM
cte_n2 a CROSS JOIN cte_n2 b
)
SELECT
STRING_AGG(t.n, ',')
FROM
cte_Tally t
WHERE
SUBSTRING(#string, t.n, 1) = '1';

How To Sum Numbers With Commas In SQL Server?

i have string field like (1100,2014,4000) i want to separate comma and sum every number with each other for instance:
1100,2014,4000
1 1 0 0
2 0 1 4
4 0 0 0
result
#first =1+2+4=7
#second= 1+0+0=1
#third=0+1+0=1
#forth=0+4+0=4
This might help to get sum of values like this :
SELECT SUM(CAST(SUBSTRING(X.A, 1, 1) AS INT)) first,
SUM(CAST(SUBSTRING(X.A, 2, 1) AS INT)) second,
SUM(CAST(SUBSTRING(X.A, 3, 1) AS INT)) third,
SUM(CAST(SUBSTRING(X.A, 4, 1) AS INT)) fourth
FROM ( SELECT '1100' A
UNION
SELECT '2014' A
UNION
SELECT '4000' A
) X
Try this,
DECLARE #Table TABLE(Value VARCHAR(20))
INSERT INTO #Table VALUES('1100,2014,4000')
DECLARE #First INT, #Second INT, #Third INT, #Fourth INT
SELECT Split.a.value('.', 'VARCHAR(100)') AS Data
INTO #temp
FROM
(
SELECT CAST ('<M>' + REPLACE(Value, ',', '</M><M>') + '</M>' AS XML) AS Value
FROM #Table
) AS A CROSS APPLY Value.nodes ('/M') AS Split(a);
SELECT #First=SUM(Data/1000)
,#Second=SUM((Data%1000)/100)
,#Third=SUM((Data%100)/10)
,#Fourth=SUM((Data%10))
FROM #temp
SELECT #First, #Second, #Third, #Fourth
DROP TABLE #temp
Following Select statement with multiple SQL CTE expressions can be used with the help of a numbers table function and a SQL split string function for splitting numbers list using "," as seperator
For further splitting each number into its numerals, I preferred to use substring function
with cte as (
select
*
from NumbersList n,
dbo.NumbersTable(1,4,1) as nt
), splitted as (
select
list,
id,
i,
substring(val,i,1) val
from cte
cross apply dbo.Split(list,',') v
)
select
distinct
list,
i,
sum(cast(val as int)) over (partition by list, i) sumOf
from splitted
The output for following entries
insert into NumbersList select '1100,2014,4000'
insert into NumbersList select '1111,2222,3456'
is as follows
This, firstly, uses Jeff Moden's DelimitedSplit8K (as I don't know what version of SQl Server you're on). Secondly, your logic seems a little off. You say that the value for your variable #third is 0+4+0, however, that's the 4th characters of the ints. Also, this assumes that all integers are 4 characters long.
WITH VTE AS(
SELECT *
FROM (VALUES('1100,2014,4000')) v(DSn)) --This is your samnple data
SELECT SUM(CONVERT(int,SUBSTRING(RIGHT('0000' + DS.Item,4),N.I, 1))) AS [Sum]
FROM VTE
CROSS APPLY dbo.DelimitedSplit8K (VTE.DSn,',') DS
CROSS APPLY (VALUES (1),(2),(3),(4)) N(I)
GROUP BY N.I;
You can use this function for all item to row in a table.
you can create this function and try then
select * from dbo.string2table('1243,1234,2343',',')
CREATE FUNCTION [dbo].[string2table]
(
#string VARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(
data VARCHAR(256)
)
BEGIN
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string)
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output (data)
VALUES(SUBSTRING(#string, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END

How to extract URL querystring parameters in SQL Server without writing a function?

I'm having trouble identifying all the querystring parameters that are used on a site. I want to write a T-SQL query that extracts all parameters and counts them, but I don't have permission to write SQL functions, so this solution isn't much help.
The field that I'm working with (Query) includes data that looks like this:
_=1457999955221
tab=profile
tab=tags&sort=votes&page=13
page=5&sort=newest&pagesize=15
...
The query I need to write would return the result:
querystring | count
___________________
_ | 1
tab | 2
sort | 2
page | 2
pagesize | 1
...
Any help is greatly appreciated.
You can borrow one of the functions from here and just inline it into the query.
An example below. I would not expect good performance. Creating a CLR function is by far the most efficient way of splitting strings prior to SQL Server 2016.
DECLARE #QueryStrings Table
(
Query VARCHAR(8000)
)
INSERT INTO #QueryStrings
VALUES
('INVALID'),
('_=1457999955221'),
('tab=profile'),
('tab=tags&sort=votes&page=13'),
('page=5&sort=newest&pagesize=15');
WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
E2(N) AS (SELECT 1 FROM E1 a, E1 b),
E4(N) AS (SELECT 1 FROM E2 a, E2 b),
E42(N) AS (SELECT 1 FROM E4 a, E2 b)
SELECT parameter, count(*)
FROM #QueryStrings qs
CROSS APPLY (SELECT SUBSTRING(qs.Query, t.N + 1, ISNULL(NULLIF(CHARINDEX('&', qs.Query, t.N + 1), 0) - t.N - 1, 8000))
FROM (SELECT 0
UNION ALL
SELECT TOP (DATALENGTH(ISNULL(qs.Query, 1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM E42) t(N)
WHERE ( SUBSTRING(qs.Query, t.N, 1) = '&'
OR t.N = 0 )) ca1(split_result)
CROSS APPLY (SELECT CHARINDEX('=',split_result)) ca2(pos)
CROSS APPLY (SELECT CASE WHEN pos > 0 THEN LEFT(split_result,pos-1) END,
CASE WHEN pos > 0 THEN SUBSTRING(split_result, pos+1,8000) END
WHERE pos > 0) ca3(parameter,value)
GROUP BY parameter
More sexy way to approach that:
DECLARE #xml xml
;WITH cte AS (
SELECT *
FROM (VALUES
('_=1457999955221'),
('tab=profile'),
('tab=tags&sort=votes&page=13'),
('page=5&sort=newest&pagesize=15')
) as T(Query))
SELECT #xml = (
SELECT CAST(
(
SELECT '<d><param>' + REPLACE(REPLACE((STUFF((
SELECT '/' + REPLACE(REPLACE(Query,'&','/'),'=','!')
FROM cte
FOR XML PATH('')
),1,1,'')),'/','</value><param>'),'!','</param><value>') + '</value></d>') as xml))
;WITH final AS (
SELECT t.v.value('.','nvarchar(20)') as querystring
FROM #xml.nodes('/d/param') as t(v)
)
SELECT querystring, COUNT(*) as [count]
FROM final
GROUP BY querystring
Result:
querystring count
-------------------- -----------
_ 1
page 2
pagesize 1
sort 2
tab 2
(5 row(s) affected)
Necromancing.
This can now be done easily with SQL Server 2016+ (13.x+)
-- Required for STRING_SPLIT:
-- ALTER DATABASE <db_name> SET COMPATIBILITY_LEVEL = 130 -- >= 130
BEGIN TRY
DECLARE #sql nvarchar(MAX);
-- https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level?view=sql-server-ver15
-- SET #sql = N'ALTER DATABASE [COR_Basic_Demo_V4] SET COMPATIBILITY_LEVEL = 130; ';
SET #sql = N'ALTER DATABASE ' + QUOTENAME(DB_NAME()) + N' SET COMPATIBILITY_LEVEL = ' + (SELECT CAST(MAX(compatibility_level) AS nvarchar(10)) FROM sys.databases) + '; ';
-- PRINT #sql;
EXECUTE(#sql);
END TRY
BEGIN CATCH
-- Execute error retrieval routine.
-- EXECUTE usp_GetErrorInfo;
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
;
END CATCH
-- Here comes the actual computation
DECLARE #input nvarchar(4000)
SET #input = N'_=1457999955221
tab=profile
tab=tags&sort=votes&page=13
page=5&sort=newest&pagesize=15'
;WITH CTE AS
(
SELECT
value
,SUBSTRING(splitted.value, 1, NULLIF(CHARINDEX('=', splitted.value), 0) -1) AS k
,SUBSTRING(splitted.value, NULLIF(CHARINDEX('=', splitted.value), 0) + 1, LEN(splitted.value)) AS v
FROM STRING_SPLIT
(
REPLACE
(
REPLACE(#input, CHAR(13), '')
,CHAR(10)
,'&'
)
, '&'
) AS splitted
)
SELECT
k
,COUNT(v) AS cnt
,COUNT(DISTINCT v) AS dist_cnt
FROM CTE
GROUP BY k
For earlier versions, or if you actually need to decompose a full-url:
DECLARE #String nvarchar(4000)
DECLARE #path nvarchar(MAX)
DECLARE #hash nvarchar(MAX)
DECLARE #Delimiter nchar(1)
SET #String = 'http://localhost:10004/Kamikatze/ajax/AnySelect.ashx?sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo#foobar'
SET #String = 'sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo#foobar'
-- SET #String = 'http://localhost:10004/Kamikatze/ajax/AnySelect.ashx?sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo'
SET #Delimiter = '&'
SELECT
#path = SUBSTRING(#String, 1, NULLIF(CHARINDEX('?', #String) - 1, -1)) -- AS path
,#hash = RIGHT(#String, LEN(#String) - NULLIF(CHARINDEX(N'#', #String), 0) ) -- AS hash
SELECT -- remove hash
#String = SUBSTRING
(
#String
,1
,COALESCE(NULLIF(CHARINDEX(N'#', #String), 0) - 1, LEN(#String) )
) -- AS xxx
;
SELECT -- remove path
#String = SUBSTRING
( #String
,CHARINDEX(N'?', #String) + 1
,100000
)
;
;WITH Split(id, stpos, endpos, data)
AS
(
SELECT
0 AS id
,0 AS stpos
,CHARINDEX(#Delimiter, #String) AS endpos
,SUBSTRING(#String, 0, COALESCE(NULLIF(CHARINDEX(#Delimiter, #String), 0), LEN(#String)+1) ) AS data
UNION ALL
SELECT
Split.id + 1 AS id
,Split.endpos + 1 AS stpos
,CHARINDEX(#Delimiter, #String, Split.endpos+1) AS endpos
,SUBSTRING(#String, Split.endpos + 1, COALESCE(NULLIF(CHARINDEX(#Delimiter, #String, Split.endpos+1), 0), LEN(#String)+1) - Split.endpos - 1) AS data
FROM Split
WHERE endpos > 0
)
SELECT
id
-- ,stpos
-- ,endpos
-- ,SUBSTRING(#String, stpos, COALESCE(NULLIF(endpos, 0), LEN(#String)+1) - stpos) AS data_simple
,data
,#path AS path
,#hash AS hash
,SUBSTRING(data, 1, NULLIF(charindex('=', data), 0) -1) AS k
,SUBSTRING(data, NULLIF(charindex('=', data), 0) + 1, LEN(data)) AS v
FROM Split
And if you want a function:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tfu_DecomposeUrl]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
EXECUTE('CREATE FUNCTION dbo.tfu_DecomposeUrl( ) RETURNS TABLE AS RETURN ( SELECT 123 AS abc) ');
GO
ALTER FUNCTION dbo.tfu_DecomposeUrl
(
#input_string NVARCHAR(4000)
)
RETURNS TABLE
AS
RETURN
(
WITH CTE AS
(
SELECT
SUBSTRING(#input_string, 1, NULLIF(CHARINDEX('?', #input_string) - 1, -1)) AS query_path
,RIGHT(#input_string, LEN(#input_string) - NULLIF(CHARINDEX(N'#', #input_string), 0) ) AS query_hash
,SUBSTRING
(
#input_string
,1
,COALESCE(NULLIF(CHARINDEX(N'#', #input_string), 0) - 1, LEN(#input_string) )
) AS PathWithoutHash
)
,CTE2 AS
(
SELECT
CTE.query_path
,CTE.query_hash
,SUBSTRING
( PathWithoutHash
,CHARINDEX(N'?', PathWithoutHash) + 1
,100000
) AS KeyValueString
FROM CTE
)
,Split(id, stpos, endpos, data, query_path, query_hash)
AS
(
SELECT
0 AS id
,0 AS stpos
,CHARINDEX(N'&', CTE2.KeyValueString) AS endpos
,SUBSTRING(CTE2.KeyValueString, 0, COALESCE(NULLIF(CHARINDEX(N'&', CTE2.KeyValueString), 0), LEN(CTE2.KeyValueString)+1) ) AS data
,CTE2.query_path
,CTE2.query_hash
FROM CTE2
UNION ALL
SELECT
Split.id + 1 AS id
,Split.endpos + 1 AS stpos
,CHARINDEX(N'&', CTE2.KeyValueString, Split.endpos+1) AS endpos
,SUBSTRING(CTE2.KeyValueString, Split.endpos + 1, COALESCE(NULLIF(CHARINDEX(N'&', CTE2.KeyValueString, Split.endpos+1), 0), LEN(CTE2.KeyValueString)+1) - Split.endpos - 1) AS data
,CTE2.query_path
,CTE2.query_hash
FROM Split
CROSS JOIN CTE2
WHERE endpos > 0
)
SELECT
id
-- ,stpos
-- ,endpos
-- ,SUBSTRING(#String, stpos, COALESCE(NULLIF(endpos, 0), LEN(#String)+1) - stpos) AS data_simple
,data
,query_path
,query_hash
,SUBSTRING(data, 1, NULLIF(CHARINDEX('=', data), 0) -1) AS k
,SUBSTRING(data, NULLIF(CHARINDEX('=', data), 0) + 1, LEN(data)) AS v
FROM Split
)
GO
Which can be simplified into this
DECLARE #input_string nvarchar(4000)
SET #input_string = 'http://localhost:10004/Kamikatze/ajax/AnySelect.ashx?sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo#foobar'
-- SET #input_string = 'sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo#foobar'
-- SET #input_string = 'http://localhost:10004/Kamikatze/ajax/AnySelect.ashx?sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo'
-- SET #input_string = 'sql=Maps.ObjectBounds.sql&BE_ID=123&obj_uid=fd4ea870-82eb-4c37-bb67-3e8d5b7b7ac2&&in_stichtag=1589528927178&no_cache=1589528927178&no_cache=1589528927178&moo=moo'
;WITH CTE AS
(
SELECT
SUBSTRING(#input_string, 1, NULLIF(CHARINDEX('?', #input_string) - 1, -1)) AS query_path
,RIGHT(#input_string, LEN(#input_string) - NULLIF(CHARINDEX(N'#', #input_string), 0) ) AS query_hash
,SUBSTRING
(
#input_string
,1
,COALESCE(NULLIF(CHARINDEX(N'#', #input_string), 0) - 1, LEN(#input_string) )
) AS PathWithoutHash
)
,CTE2 AS
(
SELECT
CTE.query_path
,CTE.query_hash
,SUBSTRING
( PathWithoutHash
,CHARINDEX(N'?', PathWithoutHash) + 1
,100000
) AS KeyValueString
FROM CTE
)
SELECT
t.id
,t.data
,CTE2.query_path
,CTE2.query_hash
,SUBSTRING(t.data, 1, NULLIF(CHARINDEX('=', t.data), 0) -1) AS k
,SUBSTRING(t.data, NULLIF(CHARINDEX('=', t.data), 0) + 1, LEN(t.data)) AS v
FROM CTE2
OUTER APPLY dbo.tfu_FastSplitString(CTE2.KeyValueString, N'&') AS t
And a table-valued string-splitting function:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.tfu_FastSplitString') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
EXECUTE('CREATE FUNCTION dbo.tfu_FastSplitString( ) RETURNS TABLE AS RETURN ( SELECT 123 AS abc) ');
GO
ALTER FUNCTION dbo.tfu_FastSplitString
(
#input_string nvarchar(4000)
,#delimiter nchar(1)
)
RETURNS TABLE
AS
RETURN
(
WITH Split(id, stpos, endpos) -- , data)
AS
(
SELECT
0 AS id
,0 AS stpos
,CHARINDEX(#delimiter, #input_string) AS endpos
-- ,SUBSTRING(#input_string, 0, COALESCE(NULLIF(CHARINDEX(#delimiter, #input_string), 0), LEN(#input_string)+1) ) AS data
UNION ALL
SELECT
Split.id + 1 AS id
,Split.endpos + 1 AS stpos
,CHARINDEX(#delimiter, #input_string, Split.endpos+1) AS endpos
-- ,SUBSTRING(#input_string, Split.endpos + 1, COALESCE(NULLIF(CHARINDEX(#delimiter, #input_string, Split.endpos+1), 0), LEN(#input_string)+1) - Split.endpos - 1) AS data
FROM Split
WHERE endpos > 0
)
SELECT
id
-- ,stpos
-- ,endpos
-- ,data
,SUBSTRING(#input_string, stpos, COALESCE(NULLIF(endpos, 0), LEN(#input_string)+1) - stpos) AS data
FROM Split
)
GO
These are inline table-valued functions, so they should be fast.
If it were a multi-statement table-valued function, it would be slow.

Replace values in a CSV string

I have a list of products in comma separated fashion and since the item list was replaced with new product items, I am trying to modify this CSV list with new product item list.
create table #tmp
(
id int identity(1,1) not null,
plist varchar(max) null
);
create table #tmpprod
(
oldid int null,
newid int null
);
insert into #tmp(plist) values
('10,11,15,17,19'),
('22,34,44,25'),
('5,6,8,9');
insert into #tmpprod(oldid, newid) values
(5, 109),
(9, 110),
(10, 111),
(15, 112),
(19, 113),
(30, 114),
(34, 222),
(44, 333);
I am trying to use a split fn to convert into rows and then replace these values and then convert columns to rows again. Is it possible in any other manner?
The output will be as:
id
newlist
1
111,11,112,17,113
2
22,222,333,25
3
109,6,8,110
Convert your comma separated list to XML. Use a numbers table, XQuery and position() to get the separate ID's with the position they have in the string. Build the comma separated string using the for xml path('') trick with a left outer join to #tempprod and order by position().
;with C as
(
select T.id,
N.number as Pos,
X.PList.value('(/i[position()=sql:column("N.Number")])[1]', 'int') as PID
from #tmp as T
cross apply (select cast('<i>'+replace(plist, ',', '</i><i>')+'</i>' as xml)) as X(PList)
inner join master..spt_values as N
on N.number between 1 and X.PList.value('count(/i)', 'int')
where N.type = 'P'
)
select C1.id,
stuff((select ','+cast(coalesce(T.newid, C2.PID) as varchar(10))
from C as C2
left outer join #tmpprod as T
on C2.PID = T.oldid
where C1.id = C2.id
order by C2.Pos
for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '')
from C as C1
group by C1.id
Try on SE-Data
Assuming SQL Server 2005 or better, and assuming order isn't important, then given this split function:
CREATE FUNCTION [dbo].[SplitInts]
(
#List VARCHAR(MAX),
#Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN ( SELECT Item FROM ( SELECT Item = x.i.value('(./text())[1]', 'int')
FROM
( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(#List, #Delimiter, '</i><i>')
+ '</i>').query('.') ) AS a CROSS APPLY [XML].nodes('i') AS x(i)
) AS y WHERE Item IS NOT NULL);
GO
You can get this result in the following way:
;WITH x AS
(
SELECT id, item, oldid, [newid], rn = ROW_NUMBER() OVER
(PARTITION BY id
ORDER BY PATINDEX('%,' + RTRIM(s.Item) + ',%', ',' + t.plist + ','))
FROM #tmp AS t CROSS APPLY dbo.SplitInts(t.plist, ',') AS s
LEFT OUTER JOIN #tmpprod AS p ON p.oldid = s.Item
)
SELECT id, newlist = STUFF((SELECT ',' + RTRIM(COALESCE([newid], Item))
FROM x AS x2 WHERE x2.id = x.id
FOR XML PATH(''),
TYPE).value(N'./text()[1]', N'varchar(max)'), 1, 1, '')
FROM x GROUP BY id;
Results:
id
newlist
1
111,11,112,17,113
2
22,222,333,25
3
109,6,8,110
Note that the ROW_NUMBER() / OVER / PARTITION BY / ORDER BY is only there to try to coerce the optimizer to return the rows in that order. You may observe this behavior today and it can change tomorrow depending on statistics or data changes, optimizer changes (service packs, CUs, upgrade, etc.) or other variables.
Long story short: if you're depending on that order, just send the set back to the client, and have the client construct the comma-delimited list. It's probably where this functionality belongs anyway.
That said, in SQL Server 2017+, we can guarantee retaining the order by splitting with OPENJSON() and reassembling with STRING_AGG():
;WITH x AS
(
SELECT o.id, val = COALESCE(n.newid, p.value), p.[key]
FROM #tmp AS o CROSS APPLY
OPENJSON('["' + REPLACE(o.pList, ',', '","') + '"]') AS p
LEFT OUTER JOIN #tmpprod AS n
ON p.value = n.oldid
)
SELECT id, newlist = STRING_AGG(val, ',')
WITHIN GROUP (ORDER BY [key])
FROM x GROUP BY id;
Example db<>fiddle
Thanks for this question - I've just learned something new. The following code is an adaptation of an article written by Rob Volk on exactly this topic. This is a very clever query! I won't copy all of the content down here. I have adapted it to create the results you're looking for in your example.
CREATE TABLE #nums (n INT)
DECLARE #i INT
SET #i = 1
WHILE #i < 8000
BEGIN
INSERT #nums VALUES(#i)
SET #i = #i + 1
END
CREATE TABLE #tmp (
id INT IDENTITY(1,1) not null,
plist VARCHAR(MAX) null
)
INSERT INTO #tmp
VALUES('10,11,15,17,19'),('22,34,44,25'),('5,6,8,9')
CREATE TABLE #tmpprod (
oldid INT NULL,
newid INT NULL
)
INSERT INTO #tmpprod VALUES(5, 109),(9, 110),(10, 111),(15, 112),(19, 113),(30, 114),(34, 222),(44, 333)
;WITH cte AS (SELECT ID, NULLIF(SUBSTRING(',' + plist + ',' , n , CHARINDEX(',' , ',' + plist + ',' , n) - n) , '') AS prod
FROM #nums, #tmp
WHERE ID <= LEN(',' + plist + ',') AND SUBSTRING(',' + plist + ',' , n - 1, 1) = ','
AND CHARINDEX(',' , ',' + plist + ',' , n) - n > 0)
UPDATE t SET plist = (SELECT CAST(CASE WHEN tp.oldid IS NULL THEN cte.prod ELSE tp.newid END AS VARCHAR) + ','
FROM cte LEFT JOIN #tmpprod tp ON cte.prod = tp.oldid
WHERE cte.id = t.id FOR XML PATH(''))
FROM #tmp t WHERE id = t.id
UPDATE #tmp SET plist = SUBSTRING(plist, 1, LEN(plist) -1)
WHERE LEN(plist) > 0 AND SUBSTRING(plist, LEN(plist), 1) = ','
SELECT * FROM #tmp
DROP TABLE #tmp
DROP TABLE #tmpprod
DROP TABLE #nums
The #nums table is a table of sequential integers, the length of which must be greater than the longest CSV you have in your table. The first 8 lines of the script create this table and populate it. Then I've copied in your code, followed by the meat of this query - the very clever single-query parser, described in more detail in the article pointed to above. The common table expression (WITH cte...) does the parsing, and the update script recompiles the results into CSV and updates #tmp.
Adam Machanic's blog contains this posting of a T-SQL only UDF which can accept T-SQL's wildcards for use in replacement.
http://dataeducation.com/splitting-a-string-of-unlimited-length/
For my own use, I adjusted the varchar sizes to max. Also note that this UDF performs rather slowly, but if you cannot use the CLR, it may be an option. The minor changes I made to the author's code may limit use of this to SQL Server 2008r2 and later.
CREATE FUNCTION dbo.PatternReplace
(
#InputString VARCHAR(max),
#Pattern VARCHAR(max),
#ReplaceText VARCHAR(max)
)
RETURNS VARCHAR(max)
AS
BEGIN
DECLARE #Result VARCHAR(max) = ''
-- First character in a match
DECLARE #First INT
-- Next character to start search on
DECLARE #Next INT = 1
-- Length of the total string -- 0 if #InputString is NULL
DECLARE #Len INT = COALESCE(LEN(#InputString), 0)
-- End of a pattern
DECLARE #EndPattern INT
WHILE (#Next <= #Len)
BEGIN
SET #First = PATINDEX('%' + #Pattern + '%', SUBSTRING(#InputString, #Next, #Len))
IF COALESCE(#First, 0) = 0 --no match - return
BEGIN
SET #Result = #Result +
CASE --return NULL, just like REPLACE, if inputs are NULL
WHEN #InputString IS NULL
OR #Pattern IS NULL
OR #ReplaceText IS NULL THEN NULL
ELSE SUBSTRING(#InputString, #Next, #Len)
END
BREAK
END
ELSE
BEGIN
-- Concatenate characters before the match to the result
SET #Result = #Result + SUBSTRING(#InputString, #Next, #First - 1)
SET #Next = #Next + #First - 1
SET #EndPattern = 1
-- Find start of end pattern range
WHILE PATINDEX(#Pattern, SUBSTRING(#InputString, #Next, #EndPattern)) = 0
SET #EndPattern = #EndPattern + 1
-- Find end of pattern range
WHILE PATINDEX(#Pattern, SUBSTRING(#InputString, #Next, #EndPattern)) > 0
AND #Len >= (#Next + #EndPattern - 1)
SET #EndPattern = #EndPattern + 1
--Either at the end of the pattern or #Next + #EndPattern = #Len
SET #Result = #Result + #ReplaceText
SET #Next = #Next + #EndPattern - 1
END
END
RETURN(#Result)
END

How do I split a delimited string so I can access individual items?

Using SQL Server, how do I split a string so I can access item x?
Take a string "Hello John Smith". How can I split the string by space and access the item at index 1 which should return "John"?
I don't believe SQL Server has a built-in split function, so other than a UDF, the only other answer I know is to hijack the PARSENAME function:
SELECT PARSENAME(REPLACE('Hello John Smith', ' ', '.'), 2)
PARSENAME takes a string and splits it on the period character. It takes a number as its second argument, and that number specifies which segment of the string to return (working from back to front).
SELECT PARSENAME(REPLACE('Hello John Smith', ' ', '.'), 3) --return Hello
Obvious problem is when the string already contains a period. I still think using a UDF is the best way...any other suggestions?
You may find the solution in SQL User Defined Function to Parse a Delimited String helpful (from The Code Project).
You can use this simple logic:
Declare #products varchar(200) = '1|20|3|343|44|6|8765'
Declare #individual varchar(20) = null
WHILE LEN(#products) > 0
BEGIN
IF PATINDEX('%|%', #products) > 0
BEGIN
SET #individual = SUBSTRING(#products,
0,
PATINDEX('%|%', #products))
SELECT #individual
SET #products = SUBSTRING(#products,
LEN(#individual + '|') + 1,
LEN(#products))
END
ELSE
BEGIN
SET #individual = #products
SET #products = NULL
SELECT #individual
END
END
First, create a function (using CTE, common table expression does away with the need for a temp table)
create function dbo.SplitString
(
#str nvarchar(4000),
#separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
select
1,
1,
charindex(#separator, #str)
union all
select
p + 1,
b + 1,
charindex(#separator, #str, b + 1)
from tokens
where b > 0
)
select
p-1 zeroBasedOccurance,
substring(
#str,
a,
case when b > 0 then b-a ELSE 4000 end)
AS s
from tokens
)
GO
Then, use it as any table (or modify it to fit within your existing stored proc) like this.
select s
from dbo.SplitString('Hello John Smith', ' ')
where zeroBasedOccurance=1
Update
Previous version would fail for input string longer than 4000 chars. This version takes care of the limitation:
create function dbo.SplitString
(
#str nvarchar(max),
#separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
select
cast(1 as bigint),
cast(1 as bigint),
charindex(#separator, #str)
union all
select
p + 1,
b + 1,
charindex(#separator, #str, b + 1)
from tokens
where b > 0
)
select
p-1 ItemIndex,
substring(
#str,
a,
case when b > 0 then b-a ELSE LEN(#str) end)
AS s
from tokens
);
GO
Usage remains the same.
Most of the solutions here use while loops or recursive CTEs. A set-based approach will be superior, I promise, if you can use a delimiter other than a space:
CREATE FUNCTION [dbo].[SplitString]
(
#List NVARCHAR(MAX),
#Delim VARCHAR(255)
)
RETURNS TABLE
AS
RETURN ( SELECT [Value], idx = RANK() OVER (ORDER BY n) FROM
(
SELECT n = Number,
[Value] = LTRIM(RTRIM(SUBSTRING(#List, [Number],
CHARINDEX(#Delim, #List + #Delim, [Number]) - [Number])))
FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
FROM sys.all_objects) AS x
WHERE Number <= LEN(#List)
AND SUBSTRING(#Delim + #List, [Number], LEN(#Delim)) = #Delim
) AS y
);
Sample usage:
SELECT Value FROM dbo.SplitString('foo,bar,blat,foo,splunge',',')
WHERE idx = 3;
Results:
----
blat
You could also add the idx you want as an argument to the function, but I'll leave that as an exercise to the reader.
You can't do this with just the native STRING_SPLIT function added in SQL Server 2016, because there is no guarantee that the output will be rendered in the order of the original list. In other words, if you pass in 3,6,1 the result will likely be in that order, but it could be 1,3,6. I have asked for the community's help in improving the built-in function here:
Please help with STRING_SPLIT improvements
With enough qualitative feedback, they may actually consider making some of these enhancements:
STRING_SPLIT is not feature complete
More on split functions, why (and proof that) while loops and recursive CTEs don't scale, and better alternatives, if splitting strings coming from the application layer:
Split strings the right way – or the next best way
Splitting Strings : A Follow-Up
Splitting Strings : Now with less T-SQL
Comparing string splitting / concatenation methods
Processing a list of integers : my approach
Splitting a list of integers : another roundup
More on splitting lists : custom delimiters, preventing duplicates, and maintaining order
Removing Duplicates from Strings in SQL Server
On SQL Server 2016 or above, though, you should look at STRING_SPLIT() and STRING_AGG():
Performance Surprises and Assumptions : STRING_SPLIT()
STRING_SPLIT() in SQL Server 2016 : Follow-Up #1
STRING_SPLIT() in SQL Server 2016 : Follow-Up #2
SQL Server v.Next : STRING_AGG() performance
Solve old problems with SQL Server’s new STRING_AGG and STRING_SPLIT functions
You can leverage a Number table to do the string parsing.
Create a physical numbers table:
create table dbo.Numbers (N int primary key);
insert into dbo.Numbers
select top 1000 row_number() over(order by number) from master..spt_values
go
Create test table with 1000000 rows
create table #yak (i int identity(1,1) primary key, array varchar(50))
insert into #yak(array)
select 'a,b,c' from dbo.Numbers n cross join dbo.Numbers nn
go
Create the function
create function [dbo].[ufn_ParseArray]
( #Input nvarchar(4000),
#Delimiter char(1) = ',',
#BaseIdent int
)
returns table as
return
( select row_number() over (order by n asc) + (#BaseIdent - 1) [i],
substring(#Input, n, charindex(#Delimiter, #Input + #Delimiter, n) - n) s
from dbo.Numbers
where n <= convert(int, len(#Input)) and
substring(#Delimiter + #Input, n, 1) = #Delimiter
)
go
Usage (outputs 3mil rows in 40s on my laptop)
select *
from #yak
cross apply dbo.ufn_ParseArray(array, ',', 1)
cleanup
drop table dbo.Numbers;
drop function [dbo].[ufn_ParseArray]
Performance here is not amazing, but calling a function over a million row table is not the best idea. If performing a string split over many rows I would avoid the function.
This question is not about a string split approach, but about how to get the nth element.
All answers here are doing some kind of string splitting using recursion, CTEs, multiple CHARINDEX, REVERSE and PATINDEX, inventing functions, call for CLR methods, number tables, CROSS APPLYs ... Most answers cover many lines of code.
But - if you really want nothing more than an approach to get the nth element - this can be done as real one-liner, no UDF, not even a sub-select... And as an extra benefit: type safe
Get part 2 delimited by a space:
DECLARE #input NVARCHAR(100)=N'part1 part2 part3';
SELECT CAST(N'<x>' + REPLACE(#input,N' ',N'</x><x>') + N'</x>' AS XML).value('/x[2]','nvarchar(max)')
Of course you can use variables for delimiter and position (use sql:column to retrieve the position directly from a query's value):
DECLARE #dlmt NVARCHAR(10)=N' ';
DECLARE #pos INT = 2;
SELECT CAST(N'<x>' + REPLACE(#input,#dlmt,N'</x><x>') + N'</x>' AS XML).value('/x[sql:variable("#pos")][1]','nvarchar(max)')
If your string might include forbidden characters (especially one among &><), you still can do it this way. Just use FOR XML PATH on your string first to replace all forbidden characters with the fitting escape sequence implicitly.
It's a very special case if - additionally - your delimiter is the semicolon. In this case I replace the delimiter first to '#DLMT#', and replace this to the XML tags finally:
SET #input=N'Some <, > and &;Other äöü#€;One more';
SET #dlmt=N';';
SELECT CAST(N'<x>' + REPLACE((SELECT REPLACE(#input,#dlmt,'#DLMT#') AS [*] FOR XML PATH('')),N'#DLMT#',N'</x><x>') + N'</x>' AS XML).value('/x[sql:variable("#pos")][1]','nvarchar(max)');
UPDATE for SQL-Server 2016+
Regretfully the developers forgot to return the part's index with STRING_SPLIT. But, using SQL-Server 2016+, there is JSON_VALUE and OPENJSON.
With JSON_VALUE we can pass in the position as the index' array.
For OPENJSON the documentation states clearly:
When OPENJSON parses a JSON array, the function returns the indexes of the elements in the JSON text as keys.
A string like 1,2,3 needs nothing more than brackets: [1,2,3].
A string of words like this is an example needs to be ["this","is","an","example"].
These are very easy string operations. Just try it out:
DECLARE #str VARCHAR(100)='Hello John Smith';
DECLARE #position INT = 2;
--We can build the json-path '$[1]' using CONCAT
SELECT JSON_VALUE('["' + REPLACE(#str,' ','","') + '"]',CONCAT('$[',#position-1,']'));
--See this for a position safe string-splitter (zero-based):
SELECT JsonArray.[key] AS [Position]
,JsonArray.[value] AS [Part]
FROM OPENJSON('["' + REPLACE(#str,' ','","') + '"]') JsonArray
In this post I tested various approaches and found, that OPENJSON is really fast. Even much faster than the famous "delimitedSplit8k()" method...
UPDATE 2 - Get the values type-safe
We can use an array within an array simply by using doubled [[]]. This allows for a typed WITH-clause:
DECLARE #SomeDelimitedString VARCHAR(100)='part1|1|20190920';
DECLARE #JsonArray NVARCHAR(MAX)=CONCAT('[["',REPLACE(#SomeDelimitedString,'|','","'),'"]]');
SELECT #SomeDelimitedString AS TheOriginal
,#JsonArray AS TransformedToJSON
,ValuesFromTheArray.*
FROM OPENJSON(#JsonArray)
WITH(TheFirstFragment VARCHAR(100) '$[0]'
,TheSecondFragment INT '$[1]'
,TheThirdFragment DATE '$[2]') ValuesFromTheArray
Here is a UDF which will do it. It will return a table of the delimited values, haven't tried all scenarios on it but your example works fine.
CREATE FUNCTION SplitString
(
-- Add the parameters for the function here
#myString varchar(500),
#deliminator varchar(10)
)
RETURNS
#ReturnTable TABLE
(
-- Add the column definitions for the TABLE variable here
[id] [int] IDENTITY(1,1) NOT NULL,
[part] [varchar](50) NULL
)
AS
BEGIN
Declare #iSpaces int
Declare #part varchar(50)
--initialize spaces
Select #iSpaces = charindex(#deliminator,#myString,0)
While #iSpaces > 0
Begin
Select #part = substring(#myString,0,charindex(#deliminator,#myString,0))
Insert Into #ReturnTable(part)
Select #part
Select #myString = substring(#mystring,charindex(#deliminator,#myString,0)+ len(#deliminator),len(#myString) - charindex(' ',#myString,0))
Select #iSpaces = charindex(#deliminator,#myString,0)
end
If len(#myString) > 0
Insert Into #ReturnTable
Select #myString
RETURN
END
GO
You would call it like this:
Select * From SplitString('Hello John Smith',' ')
Edit: Updated solution to handle delimters with a len>1 as in :
select * From SplitString('Hello**John**Smith','**')
Here I post a simple way of solution
CREATE FUNCTION [dbo].[split](
#delimited NVARCHAR(MAX),
#delimiter NVARCHAR(100)
) RETURNS #t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE #xml XML
SET #xml = N'<t>' + REPLACE(#delimited,#delimiter,'</t><t>') + '</t>'
INSERT INTO #t(val)
SELECT r.value('.','varchar(MAX)') as item
FROM #xml.nodes('/t') as records(r)
RETURN
END
Execute the function like this
select * from dbo.split('Hello John Smith',' ')
In my opinion you guys are making it way too complicated. Just create a CLR UDF and be done with it.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;
public partial class UserDefinedFunctions {
[SqlFunction]
public static SqlString SearchString(string Search) {
List<string> SearchWords = new List<string>();
foreach (string s in Search.Split(new char[] { ' ' })) {
if (!s.ToLower().Equals("or") && !s.ToLower().Equals("and")) {
SearchWords.Add(s);
}
}
return new SqlString(string.Join(" OR ", SearchWords.ToArray()));
}
};
What about using string and values() statement?
DECLARE #str varchar(max)
SET #str = 'Hello John Smith'
DECLARE #separator varchar(max)
SET #separator = ' '
DECLARE #Splited TABLE(id int IDENTITY(1,1), item varchar(max))
SET #str = REPLACE(#str, #separator, '''),(''')
SET #str = 'SELECT * FROM (VALUES(''' + #str + ''')) AS V(A)'
INSERT INTO #Splited
EXEC(#str)
SELECT * FROM #Splited
Result-set achieved.
id item
1 Hello
2 John
3 Smith
I use the answer of frederic but this did not work in SQL Server 2005
I modified it and I'm using select with union all and it works
DECLARE #str varchar(max)
SET #str = 'Hello John Smith how are you'
DECLARE #separator varchar(max)
SET #separator = ' '
DECLARE #Splited table(id int IDENTITY(1,1), item varchar(max))
SET #str = REPLACE(#str, #separator, ''' UNION ALL SELECT ''')
SET #str = ' SELECT ''' + #str + ''' '
INSERT INTO #Splited
EXEC(#str)
SELECT * FROM #Splited
And the result-set is:
id item
1 Hello
2 John
3 Smith
4 how
5 are
6 you
This pattern works fine and you can generalize
Convert(xml,'<n>'+Replace(FIELD,'.','</n><n>')+'</n>').value('(/n[INDEX])','TYPE')
^^^^^ ^^^^^ ^^^^
note FIELD, INDEX and TYPE.
Let some table with identifiers like
sys.message.1234.warning.A45
sys.message.1235.error.O98
....
Then, you can write
SELECT Source = q.value('(/n[1])', 'varchar(10)'),
RecordType = q.value('(/n[2])', 'varchar(20)'),
RecordNumber = q.value('(/n[3])', 'int'),
Status = q.value('(/n[4])', 'varchar(5)')
FROM (
SELECT q = Convert(xml,'<n>'+Replace(fieldName,'.','</n><n>')+'</n>')
FROM some_TABLE
) Q
splitting and casting all parts.
Yet another get n'th part of string by delimeter function:
create function GetStringPartByDelimeter (
#value as nvarchar(max),
#delimeter as nvarchar(max),
#position as int
) returns NVARCHAR(MAX)
AS BEGIN
declare #startPos as int
declare #endPos as int
set #endPos = -1
while (#position > 0 and #endPos != 0) begin
set #startPos = #endPos + 1
set #endPos = charindex(#delimeter, #value, #startPos)
if(#position = 1) begin
if(#endPos = 0)
set #endPos = len(#value) + 1
return substring(#value, #startPos, #endPos - #startPos)
end
set #position = #position - 1
end
return null
end
and the usage:
select dbo.GetStringPartByDelimeter ('a;b;c;d;e', ';', 3)
which returns:
c
If your database has compatibility level of 130 or higher then you can use the STRING_SPLIT function along with OFFSET FETCH clauses to get the specific item by index.
To get the item at index N (zero based), you can use the following code
SELECT value
FROM STRING_SPLIT('Hello John Smith',' ')
ORDER BY (SELECT NULL)
OFFSET N ROWS
FETCH NEXT 1 ROWS ONLY
To check the compatibility level of your database, execute this code:
SELECT compatibility_level
FROM sys.databases WHERE name = 'YourDBName';
Try this:
CREATE function [SplitWordList]
(
#list varchar(8000)
)
returns #t table
(
Word varchar(50) not null,
Position int identity(1,1) not null
)
as begin
declare
#pos int,
#lpos int,
#item varchar(100),
#ignore varchar(100),
#dl int,
#a1 int,
#a2 int,
#z1 int,
#z2 int,
#n1 int,
#n2 int,
#c varchar(1),
#a smallint
select
#a1 = ascii('a'),
#a2 = ascii('A'),
#z1 = ascii('z'),
#z2 = ascii('Z'),
#n1 = ascii('0'),
#n2 = ascii('9')
set #ignore = '''"'
set #pos = 1
set #dl = datalength(#list)
set #lpos = 1
set #item = ''
while (#pos <= #dl) begin
set #c = substring(#list, #pos, 1)
if (#ignore not like '%' + #c + '%') begin
set #a = ascii(#c)
if ((#a >= #a1) and (#a <= #z1))
or ((#a >= #a2) and (#a <= #z2))
or ((#a >= #n1) and (#a <= #n2))
begin
set #item = #item + #c
end else if (#item > '') begin
insert into #t values (#item)
set #item = ''
end
end
set #pos = #pos + 1
end
if (#item > '') begin
insert into #t values (#item)
end
return
end
Test it like this:
select * from SplitWordList('Hello John Smith')
I was looking for the solution on net and the below works for me.
Ref.
And you call the function like this :
SELECT * FROM dbo.split('ram shyam hari gopal',' ')
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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
The following example uses a recursive CTE
Update 18.09.2013
CREATE FUNCTION dbo.SplitStrings_CTE(#List nvarchar(max), #Delimiter nvarchar(1))
RETURNS #returns TABLE (val nvarchar(max), [level] int, PRIMARY KEY CLUSTERED([level]))
AS
BEGIN
;WITH cte AS
(
SELECT SUBSTRING(#List, 0, CHARINDEX(#Delimiter, #List + #Delimiter)) AS val,
CAST(STUFF(#List + #Delimiter, 1, CHARINDEX(#Delimiter, #List + #Delimiter), '') AS nvarchar(max)) AS stval,
1 AS [level]
UNION ALL
SELECT SUBSTRING(stval, 0, CHARINDEX(#Delimiter, stval)),
CAST(STUFF(stval, 1, CHARINDEX(#Delimiter, stval), '') AS nvarchar(max)),
[level] + 1
FROM cte
WHERE stval != ''
)
INSERT #returns
SELECT REPLACE(val, ' ','' ) AS val, [level]
FROM cte
WHERE val > ''
RETURN
END
Demo on SQLFiddle
Alter Function dbo.fn_Split
(
#Expression nvarchar(max),
#Delimiter nvarchar(20) = ',',
#Qualifier char(1) = Null
)
RETURNS #Results TABLE (id int IDENTITY(1,1), value nvarchar(max))
AS
BEGIN
/* USAGE
Select * From dbo.fn_Split('apple pear grape banana orange honeydew cantalope 3 2 1 4', ' ', Null)
Select * From dbo.fn_Split('1,abc,"Doe, John",4', ',', '"')
Select * From dbo.fn_Split('Hello 0,"&""&&&&', ',', '"')
*/
-- Declare Variables
DECLARE
#X xml,
#Temp nvarchar(max),
#Temp2 nvarchar(max),
#Start int,
#End int
-- HTML Encode #Expression
Select #Expression = (Select #Expression For XML Path(''))
-- Find all occurences of #Delimiter within #Qualifier and replace with |||***|||
While PATINDEX('%' + #Qualifier + '%', #Expression) > 0 AND Len(IsNull(#Qualifier, '')) > 0
BEGIN
Select
-- Starting character position of #Qualifier
#Start = PATINDEX('%' + #Qualifier + '%', #Expression),
-- #Expression starting at the #Start position
#Temp = SubString(#Expression, #Start + 1, LEN(#Expression)-#Start+1),
-- Next position of #Qualifier within #Expression
#End = PATINDEX('%' + #Qualifier + '%', #Temp) - 1,
-- The part of Expression found between the #Qualifiers
#Temp2 = Case When #End &LT 0 Then #Temp Else Left(#Temp, #End) End,
-- New #Expression
#Expression = REPLACE(#Expression,
#Qualifier + #Temp2 + Case When #End &LT 0 Then '' Else #Qualifier End,
Replace(#Temp2, #Delimiter, '|||***|||')
)
END
-- Replace all occurences of #Delimiter within #Expression with '&lt/fn_Split&gt&ltfn_Split&gt'
-- And convert it to XML so we can select from it
SET
#X = Cast('&ltfn_Split&gt' +
Replace(#Expression, #Delimiter, '&lt/fn_Split&gt&ltfn_Split&gt') +
'&lt/fn_Split&gt' as xml)
-- Insert into our returnable table replacing '|||***|||' back to #Delimiter
INSERT #Results
SELECT
"Value" = LTRIM(RTrim(Replace(C.value('.', 'nvarchar(max)'), '|||***|||', #Delimiter)))
FROM
#X.nodes('fn_Split') as X(C)
-- Return our temp table
RETURN
END
You can split a string in SQL without needing a function:
DECLARE #bla varchar(MAX)
SET #bla = 'BED40DFC-F468-46DD-8017-00EF2FA3E4A4,64B59FC5-3F4D-4B0E-9A48-01F3D4F220B0,A611A108-97CA-42F3-A2E1-057165339719,E72D95EA-578F-45FC-88E5-075F66FD726C'
-- http://stackoverflow.com/questions/14712864/how-to-query-values-from-xml-nodes
SELECT
x.XmlCol.value('.', 'varchar(36)') AS val
FROM
(
SELECT
CAST('<e>' + REPLACE(#bla, ',', '</e><e>') + '</e>' AS xml) AS RawXml
) AS b
CROSS APPLY b.RawXml.nodes('e') x(XmlCol);
If you need to support arbitrary strings (with xml special characters)
DECLARE #bla NVARCHAR(MAX)
SET #bla = '<html>unsafe & safe Utf8CharsDon''tGetEncoded ÄöÜ - "Conex"<html>,Barnes & Noble,abc,def,ghi'
-- http://stackoverflow.com/questions/14712864/how-to-query-values-from-xml-nodes
SELECT
x.XmlCol.value('.', 'nvarchar(MAX)') AS val
FROM
(
SELECT
CAST('<e>' + REPLACE((SELECT #bla FOR XML PATH('')), ',', '</e><e>') + '</e>' AS xml) AS RawXml
) AS b
CROSS APPLY b.RawXml.nodes('e') x(XmlCol);
In Azure SQL Database (based on Microsoft SQL Server but not exactly the same thing) the signature of STRING_SPLIT function looks like:
STRING_SPLIT ( string , separator [ , enable_ordinal ] )
When enable_ordinal flag is set to 1 the result will include a column named ordinal that consists of the 1‑based position of the substring within the input string:
SELECT *
FROM STRING_SPLIT('hello john smith', ' ', 1)
| value | ordinal |
|-------|---------|
| hello | 1 |
| john | 2 |
| smith | 3 |
This allows us to do this:
SELECT value
FROM STRING_SPLIT('hello john smith', ' ', 1)
WHERE ordinal = 2
| value |
|-------|
| john |
If enable_ordinal is not available then there is a trick which assumes that the substrings within the input string are unique. In this scenario, CHAR_INDEX could be used to find the position of the substring within the input string:
SELECT value, ROW_NUMBER() OVER (ORDER BY CHARINDEX(value, input_str)) AS ord_pos
FROM (VALUES
('hello john smith')
) AS x(input_str)
CROSS APPLY STRING_SPLIT(input_str, ' ')
| value | ord_pos |
|-------+---------|
| hello | 1 |
| john | 2 |
| smith | 3 |
I know it's an old Question, but i think some one can benefit from my solution.
select
SUBSTRING(column_name,1,CHARINDEX(' ',column_name,1)-1)
,SUBSTRING(SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name))
,1
,CHARINDEX(' ',SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name)),1)-1)
,SUBSTRING(SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name))
,CHARINDEX(' ',SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name)),1)+1
,LEN(column_name))
from table_name
SQL FIDDLE
Advantages:
It separates all the 3 sub-strings deliminator by ' '.
One must not use while loop, as it decreases the performance.
No need to Pivot as all the resultant sub-string will be displayed in
one Row
Limitations:
One must know the total no. of spaces (sub-string).
Note: the solution can give sub-string up to to N.
To overcame the limitation we can use the following ref.
But again the above solution can't be use in a table (Actaully i wasn't able to use it).
Again i hope this solution can help some-one.
Update: In case of Records > 50000 it is not advisable to use LOOPS as it will degrade the Performance
Pure set-based solution using TVF with recursive CTE. You can JOIN and APPLY this function to any dataset.
create function [dbo].[SplitStringToResultSet] (#value varchar(max), #separator char(1))
returns table
as return
with r as (
select value, cast(null as varchar(max)) [x], -1 [no] from (select rtrim(cast(#value as varchar(max))) [value]) as j
union all
select right(value, len(value)-case charindex(#separator, value) when 0 then len(value) else charindex(#separator, value) end) [value]
, left(r.[value], case charindex(#separator, r.value) when 0 then len(r.value) else abs(charindex(#separator, r.[value])-1) end ) [x]
, [no] + 1 [no]
from r where value > '')
select ltrim(x) [value], [no] [index] from r where x is not null;
go
Usage:
select *
from [dbo].[SplitStringToResultSet]('Hello John Smith', ' ')
where [index] = 1;
Result:
value index
-------------
John 1
Almost all the other answers are replacing the string being split which wastes CPU cycles and performs unnecessary memory allocations.
I cover a much better way to do a string split here: http://www.digitalruby.com/split-string-sql-server/
Here is the code:
SET NOCOUNT ON
-- You will want to change nvarchar(MAX) to nvarchar(50), varchar(50) or whatever matches exactly with the string column you will be searching against
DECLARE #SplitStringTable TABLE (Value nvarchar(MAX) NOT NULL)
DECLARE #StringToSplit nvarchar(MAX) = 'your|string|to|split|here'
DECLARE #SplitEndPos int
DECLARE #SplitValue nvarchar(MAX)
DECLARE #SplitDelim nvarchar(1) = '|'
DECLARE #SplitStartPos int = 1
SET #SplitEndPos = CHARINDEX(#SplitDelim, #StringToSplit, #SplitStartPos)
WHILE #SplitEndPos > 0
BEGIN
SET #SplitValue = SUBSTRING(#StringToSplit, #SplitStartPos, (#SplitEndPos - #SplitStartPos))
INSERT #SplitStringTable (Value) VALUES (#SplitValue)
SET #SplitStartPos = #SplitEndPos + 1
SET #SplitEndPos = CHARINDEX(#SplitDelim, #StringToSplit, #SplitStartPos)
END
SET #SplitValue = SUBSTRING(#StringToSplit, #SplitStartPos, 2147483647)
INSERT #SplitStringTable (Value) VALUES(#SplitValue)
SET NOCOUNT OFF
-- You can select or join with the values in #SplitStringTable at this point.
Recursive CTE solution with server pain, test it
MS SQL Server 2008 Schema Setup:
create table Course( Courses varchar(100) );
insert into Course values ('Hello John Smith');
Query 1:
with cte as
( select
left( Courses, charindex( ' ' , Courses) ) as a_l,
cast( substring( Courses,
charindex( ' ' , Courses) + 1 ,
len(Courses ) ) + ' '
as varchar(100) ) as a_r,
Courses as a,
0 as n
from Course t
union all
select
left(a_r, charindex( ' ' , a_r) ) as a_l,
substring( a_r, charindex( ' ' , a_r) + 1 , len(a_R ) ) as a_r,
cte.a,
cte.n + 1 as n
from Course t inner join cte
on t.Courses = cte.a and len( a_r ) > 0
)
select a_l, n from cte
--where N = 1
Results:
| A_L | N |
|--------|---|
| Hello | 0 |
| John | 1 |
| Smith | 2 |
while similar to the xml based answer by josejuan, i found that processing the xml path only once, then pivoting was moderately more efficient:
select ID,
[3] as PathProvidingID,
[4] as PathProvider,
[5] as ComponentProvidingID,
[6] as ComponentProviding,
[7] as InputRecievingID,
[8] as InputRecieving,
[9] as RowsPassed,
[10] as InputRecieving2
from
(
select id,message,d.* from sysssislog cross apply (
SELECT Item = y.i.value('(./text())[1]', 'varchar(200)'),
row_number() over(order by y.i) as rn
FROM
(
SELECT x = CONVERT(XML, '<i>' + REPLACE(Message, ':', '</i><i>') + '</i>').query('.')
) AS a CROSS APPLY x.nodes('i') AS y(i)
) d
WHERE event
=
'OnPipelineRowsSent'
) as tokens
pivot
( max(item) for [rn] in ([3],[4],[5],[6],[7],[8],[9],[10])
) as data
ran in 8:30
select id,
tokens.value('(/n[3])', 'varchar(100)')as PathProvidingID,
tokens.value('(/n[4])', 'varchar(100)') as PathProvider,
tokens.value('(/n[5])', 'varchar(100)') as ComponentProvidingID,
tokens.value('(/n[6])', 'varchar(100)') as ComponentProviding,
tokens.value('(/n[7])', 'varchar(100)') as InputRecievingID,
tokens.value('(/n[8])', 'varchar(100)') as InputRecieving,
tokens.value('(/n[9])', 'varchar(100)') as RowsPassed
from
(
select id, Convert(xml,'<n>'+Replace(message,'.','</n><n>')+'</n>') tokens
from sysssislog
WHERE event
=
'OnPipelineRowsSent'
) as data
ran in 9:20
CREATE FUNCTION [dbo].[fnSplitString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string)
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output (splitdata)
VALUES(SUBSTRING(#string, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END
AND USE IT
select *from dbo.fnSplitString('Querying SQL Server','')
if anyone wants to get only one part of the seperatured text can use this
select * from fromSplitStringSep('Word1 wordr2 word3',' ')
CREATE function [dbo].[SplitStringSep]
(
#str nvarchar(4000),
#separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
select
1,
1,
charindex(#separator, #str)
union all
select
p + 1,
b + 1,
charindex(#separator, #str, b + 1)
from tokens
where b > 0
)
select
p-1 zeroBasedOccurance,
substring(
#str,
a,
case when b > 0 then b-a ELSE 4000 end)
AS s
from tokens
)
I devoloped this,
declare #x nvarchar(Max) = 'ali.veli.deli.';
declare #item nvarchar(Max);
declare #splitter char='.';
while CHARINDEX(#splitter,#x) != 0
begin
set #item = LEFT(#x,CHARINDEX(#splitter,#x))
set #x = RIGHT(#x,len(#x)-len(#item) )
select #item as item, #x as x;
end
the only attention you should is dot '.' that end of the #x is always should be there.
building on #NothingsImpossible solution, or, rather, comment on the most voted answer (just below the accepted one), i found the following quick-and-dirty solution fulfill my own needs - it has a benefit of being solely within SQL domain.
given a string "first;second;third;fourth;fifth", say, I want to get the third token. this works only if we know how many tokens the string is going to have - in this case it's 5. so my way of action is to chop the last two tokens away (inner query), and then to chop the first two tokens away (outer query)
i know that this is ugly and covers the specific conditions i was in, but am posting it just in case somebody finds it useful. cheers
select
REVERSE(
SUBSTRING(
reverse_substring,
0,
CHARINDEX(';', reverse_substring)
)
)
from
(
select
msg,
SUBSTRING(
REVERSE(msg),
CHARINDEX(
';',
REVERSE(msg),
CHARINDEX(
';',
REVERSE(msg)
)+1
)+1,
1000
) reverse_substring
from
(
select 'first;second;third;fourth;fifth' msg
) a
) b
declare #strng varchar(max)='hello john smith'
select (
substring(
#strng,
charindex(' ', #strng) + 1,
(
(charindex(' ', #strng, charindex(' ', #strng) + 1))
- charindex(' ',#strng)
)
))