Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
CREATE TABLE Numbers (Num int identity(1, 1) NOT NULL CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED)
SET NOCOUNT ON
INSERT Numbers DEFAULT VALUES
WHILE Scope_Identity() < 8000 INSERT Numbers DEFAULT VALUES
[blue]CREATE FUNCTION[/blue] SplitNumbers [gray]([/gray]
@InputText [blue]varchar[/blue][gray]([/gray]8000[gray]),[/gray]
@Delimiter [blue]varchar[/blue][gray]([/gray]1[gray])[/gray]
[gray])[/gray]
[blue]RETURNS TABLE
AS
RETURN [/blue][gray]([/gray]
[blue]SELECT[/blue]
[color #007f7f]--This first column will drastically hurt performance[/color]
TokenID [gray]=[/gray] Num [gray]-[/gray] [color #ff40ff]Datalength[/color][gray]([/gray]Replace[gray]([/gray][color #ff40ff]Left[/color][gray]([/gray]@InputText, Num [gray]-[/gray] 1[gray]),[/gray] @Delimiter, [color #ff40ff]''[/color][gray])),[/gray]
Value [gray]=[/gray] [color #ff40ff]Substring[/color][gray]([/gray]@InputText, Num, [color #ff40ff]CharIndex[/color][gray]([/gray]@Delimiter, @InputText [gray]+[/gray] @Delimiter, Num) [gray]-[/gray] Num[gray])[/gray]
[blue]FROM[/blue] Numbers
[blue]WHERE[/blue]
[color #ff40ff]Substring[/color][gray]([/gray]@Delimiter [gray]+[/gray] @InputText[gray],[/gray] Num[gray],[/gray] 1[gray]) =[/gray] @Delimiter
[blue]AND [/blue] Num [gray]<=[/gray] [color #ff40ff]Datalength[/color][gray]([/gray]@InputText[gray]) +[/gray] 1
[gray])[/gray]
[blue]CREATE FUNCTION[/blue] SplitFixed [gray]([/gray]
@InputText [blue]text[/blue][gray],[/gray]
@TokenLength [blue]tinyint[/blue]
[gray])[/gray]
[blue]RETURNS TABLE
AS
RETURN [/blue][gray]([/gray]
[blue]SELECT[/blue]
TokenID [gray]=[/gray] N1.Num [gray]+[/gray] MaxNum [gray]* ([/gray]N2.Num [gray]-[/gray] 1[gray]),[/gray]
Value =
SubString[gray]([/gray]
@InputText[gray],[/gray]
@TokenLength [gray]* ([/gray]N1.Num [gray]+[/gray] MaxNum [gray]* ([/gray]N2.Num [gray]-[/gray] 1[gray])[/gray] [gray]-[/gray] 1[gray]) +[/gray] 1[gray],[/gray]
@TokenLength
[gray])[/gray]
[blue]FROM[/blue]
Numbers N1
[gray]CROSS JOIN ([/gray]
[blue]SELECT[/blue] MaxNum [gray]=[/gray] Max[gray]([/gray]Num[gray])[/gray] [blue]FROM[/blue] Numbers
[gray])[/gray] M
[gray]JOIN[/gray] Numbers N2 ON
@TokenLength [gray]* ([/gray]M.MaxNum [gray]* ([/gray]N2.Num [gray]-[/gray] 1[gray]) +[/gray] N1.Num [gray]-[/gray] 1[gray]) +[/gray] 1 [gray]<=[/gray] [color #ff40ff]DataLength[/color][gray]([/gray]@InputText[gray])[/gray]
[blue]WHERE[/blue]
N2.Num [gray]<=[/gray] [color #ff40ff]DataLength[/color][gray]([/gray]@InputText[gray]) / ([/gray]MaxNum [gray]*[/gray] @TokenLength[gray]) +[/gray] 1
[gray]AND[/gray] N1.Num [gray]<=[/gray]
[color #ff40ff]CASE[/color]
[blue]WHEN[/blue] [color #ff40ff]DataLength[/color][gray]([/gray]@InputText[gray]) /[/gray] @TokenLength [gray]<=[/gray] MaxNum
[blue]THEN[/blue] [color #ff40ff]DataLength[/color][gray]([/gray]@InputText[gray]) /[/gray] @TokenLength
[gray]+[/gray] [color #ff40ff]CASE DataLength[/color][gray]([/gray]@InputText[gray]) %[/gray] @TokenLength
[blue]WHEN[/blue] 0 [blue]THEN[/blue] 0
[blue]ELSE[/blue] 1
[blue]END[/blue]
[blue]ELSE[/blue] MaxNum
[blue]END[/blue]
[gray])[/gray]