-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfn_splitByDelim.sql
49 lines (46 loc) · 1.34 KB
/
fn_splitByDelim.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
USE Utility;
GO
IF OBJECT_ID(N'dbo.fn_splitByDelim') IS NOT NULL
DROP FUNCTION dbo.fn_splitByDelim;
GO
CREATE FUNCTION dbo.fn_splitByDelim
(
@strSplit varchar(MAX)
,@strDelim varchar(10)
)
RETURNS TABLE
AS
RETURN (
WITH cteSplit
AS (
SELECT [intBegin] = 1
,[intEnd] = CAST(COALESCE(NULLIF(CHARINDEX(strDelim, strSplit, 1), 0), intLength) as int)
,[strValue] = SUBSTRING(strSplit, 1, COALESCE(NULLIF(CHARINDEX(strDelim, strSplit, 1), 0), intLength) - 1)
,tbl.intLength
,tbl.intDelim
,tbl.strDelim
,tbl.strSplit
,[intId] = 1
FROM (
SELECT [intLength] = DATALENGTH(@strSplit) + 1
,[intDelim] = 1
,[strDelim] = @strDelim
,[strSplit] = @strSplit
) tbl
UNION ALL
SELECT [intBegin] = cS.intEnd + cS.intDelim
,[intEnd] = CAST(COALESCE(NULLIF(CHARINDEX(cS.strDelim, cS.strSplit, cS.intEnd + cS.intDelim), 0), cS.intLength) as int)
,[strValue] = SUBSTRING(cS.strSplit, cS.intEnd + cS.intDelim, COALESCE(NULLIF(CHARINDEX(cS.strDelim, cS.strSplit, cS.intEnd + cS.intDelim), 0), cS.intLength) - cS.intEnd - cS.intDelim)
,cS.intLength
,cS.intDelim
,cS.strDelim
,cS.strSplit
,[intId] = cS.intId + 1
FROM cteSplit cS
WHERE cS.intEnd < cS.intLength
)
SELECT strValue
,intId
FROM cteSplit
);
GO