forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathudf_FindPosition.sql
More file actions
37 lines (35 loc) · 816 Bytes
/
udf_FindPosition.sql
File metadata and controls
37 lines (35 loc) · 816 Bytes
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
CREATE FUNCTION dbo.udf_FindPosition
(
@strInput varchar ( 8000
)
,
@delimiter varchar ( 50
)
)
RETURNS TABLE
AS
RETURN
WITH findchar ( posnum,
pos
)
AS (
SELECT 1 AS posnum,
CHARINDEX ( @delimiter, @strInput
) AS pos
UNION ALL
SELECT f.posnum + 1 AS posnum,
CHARINDEX ( @delimiter, @strInput, f.pos + 1
) AS pos
FROM findchar AS f
WHERE f.posnum + 1 <= LEN ( @strInput
)
AND
pos <> 0
)
SELECT posnum
,
pos
FROM findchar
WHERE
pos > 0;
GO