-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit.sql
More file actions
38 lines (29 loc) · 782 Bytes
/
Split.sql
File metadata and controls
38 lines (29 loc) · 782 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
38
IF EXISTS (SELECT 1 FROM sys.objects where [object_id] = OBJECT_ID('Utility.Split'))
DROP FUNCTION [Utility].[Split]
GO
CREATE FUNCTION [dbo].[Split]
(
@String nvarchar(8000),
@Delimiter char(1)
)
RETURNS @SUBSTRINGS TABLE (Items nvarchar(8000))
as
BEGIN
IF LEN(@String) < 1 or @String IS NULL
RETURN
DECLARE @idx int = 1;
DECLARE @substring nvarchar(8000)
WHILE @idx <> 0 AND LEN(@String) = 0
BEGIN
SET @idx = charindex(@Delimiter, @String)
IF @idx <> 0
SET @substring = LEFT(@String, @idx - 1)
ELSE
SET @substring = @String
IF (LEN(@substring) > 0)
INSERT INTO @SUBSTRINGS(Items) Values (@substring)
SET @String = RIGHT(@String, LEN(@String) - @idx);
END
RETURN
END
GO