@@ -8,15 +8,15 @@ CREATE FUNCTION [dbo].[udf_PatternSplitLoop]
8
8
) RETURNS
9
9
@Results TABLE ( ItemNumber INT
10
10
,Item VARCHAR (400 )
11
- ,[Matched] INT )
11
+ ,[Matched] INT )
12
12
WITH SCHEMABINDING
13
13
AS
14
14
BEGIN ;
15
15
16
- -- DECLARE a couple of variables we'll need in our loop
16
+ -- DECLARE a couple of variables we'll need in our loop
17
17
DECLARE
18
18
@ItemNumber INT = 0
19
- , @Remaining VARCHAR (400 ) = ISNULL(@String, ' ' )
19
+ , @Remaining VARCHAR (400 ) = ISNULL(@String, ' ' )
20
20
-- Create the "not pattern"
21
21
, @NotPattern VARCHAR (500 ) = REPLACE(REPLACE(@Pattern, ' [' , ' [^' ), ' ^^' , ' ' )
22
22
, @Matched INT
@@ -27,14 +27,15 @@ IF @String IS NULL OR @Pattern IS NULL
27
27
WHILE DATALENGTH(@Remaining) > 0
28
28
BEGIN
29
29
SELECT @ItemNumber = @ItemNumber + 1
30
- -- The item returned from the cascaded CROSS APPLY b below ,@String = CASE
31
- -- When a+b = 1, then either a=1 and b=0 (the pattern was found but not pattern
32
- -- was not found) or a=0 and b=1 (the not pattern was found but pattern was
30
+ -- The item returned from the cascaded CROSS APPLY b below
31
+ ,@String = CASE
32
+ -- When a+b = 1, then either a=1 and b=0 (the pattern was found but not pattern
33
+ -- was not found) or a=0 and b=1 (the not pattern was found but pattern was
33
34
-- not found).
34
- -- This means that no meaninful patterns are found in what remains so we’re done.
35
- WHEN a+ b = 1 THEN @Remaining
36
- -- This case returns the chunk up to the start of the next pattern/not pattern
37
- WHEN (a= 1 AND b> 0 ) OR (b= 1 AND a> 0 ) THEN SUBSTRING (@Remaining, 1 , CASE a
35
+ -- This means that no meaninful patterns are found in what remains so we’re done.
36
+ WHEN a+ b = 1 THEN @Remaining
37
+ -- This case returns the chunk up to the start of the next pattern/not pattern
38
+ WHEN (a= 1 AND b> 0 ) OR (b= 1 AND a> 0 ) THEN SUBSTRING (@Remaining, 1 , CASE a
38
39
WHEN 1 THEN b
39
40
ELSE a
40
41
END - 1 )
@@ -49,10 +50,10 @@ WHILE DATALENGTH(@Remaining) > 0
49
50
-- Now that we have our ItemNumber and Item (in @String) INSERT them into our results
50
51
INSERT INTO @Results SELECT @ItemNumber, @String, @Matched
51
52
52
- -- Find the remaining characters in the string
53
+ -- Find the remaining characters in the string
53
54
SELECT @Remaining = CASE
54
55
WHEN DATALENGTH(@Remaining) = DATALENGTH(@String) THEN ' '
55
- ELSE SUBSTRING (@Remaining, DATALENGTH(@String)+ 1 , DATALENGTH(@Remaining))
56
+ ELSE SUBSTRING (@Remaining, DATALENGTH(@String)+ 1 , DATALENGTH(@Remaining))
56
57
END
57
58
END
58
59
0 commit comments