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 FUNCTION Split
(
@delimited nvarchar(max),
@delimiter nvarchar(20)
) RETURNS @t TABLE
(
val nvarchar(max)
)
AS
BEGIN
declare @xml xml
set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'
insert into @t(val)
select
r.value('.','varchar(5)') as item
from @xml.nodes('//root/r') as records(r)
RETURN
END
select val
from dbo.split('one two three four five',' ')
where val <> ''
select top 1 replace(
(select top 3 val as 'data()'
from (
select val
from dbo.split('one two three four five',' ')
where val <> ''
) as t1
for xml path('')),' ',' ')
one two three
ALTER FUNCTION GetShorterString
(@STR VARCHAR(8000),
@separator VARCHAR(16)=' ',
@NumOfWords int = 30)
RETURNS varchar(8000)
AS
/* Splits passed string into items based on the specified separator string
Parameters:
@str - The string to split
@separator - The separator string ( space is default)
@NumOfWords - number of items to retrieve
Returns string
*/
BEGIN
DECLARE @Item VARCHAR(128), @pos INT, @FinalResult varchar(8000), @ItemNumber int
set @FinalResult = ''
set @ItemNumber = 0
WHILE DATALENGTH(@STR) > 0 AND @ItemNumber < @NumOfWords
BEGIN
SET @pos = CHARINDEX(@separator, @STR)
IF @pos = 0
SET @pos = DATALENGTH(@STR)+1
set @ItemNumber = @ItemNumber + 1
SET @Item = LEFT(@STR, @pos -1 )
SET @STR = SUBSTRING(@STR, @pos + DATALENGTH(@separator), 8000)
set @FinalResult = @FinalResult + @separator + @Item
END
RETURN substring(@FinalResult,2,len(@FinalResult))
END