Quantcast
Channel: Answers for "Function for splitting sentence"
Viewing all articles
Browse latest Browse all 6

Answer by WBenhart

$
0
0
I know this is a really old post, but I was looking for the same kind of thing and couldn't find any function to do it. I'm really new to SQL and not really a programmer but I was able to create this function to return what I wanted. CREATE FUNCTION [dbo].[TextSplit] (@Sentence VarChar(MAX), @Pos1 AS INT, @Len AS INT) RETURNS VarChar(MAX) AS BEGIN -- Created by Wayne Benhart on September 9, 2013 -- Benhart Solutions v1.0' -- Wayne Benhart' -- WBenhart@BenhartSolutions.com' -- www.Pentagon2000Forum.com' -- -- USAGE: dbo.TextSplit(, , ) -- EXAMPLE: -- DECLARE @Str AS VARCHAR(MAX) -- SET @Str = 'This is my test of a segmented sentence.' -- SELECT dbo.TextSplit(@Str, 0, 12) AS [Line One], -- dbo.TextSplit(@Str, 13, 12) AS [Line Two], -- dbo.TextSplit(@Str, 25, 12) AS [Line Three] -- -- RESULTS: -- Line One Line Two Line Three -- This is my test of a segmented sentence. -- -- NOTE: This does not return exactly 12 characters, -- it will return the complete last word found -- and then return. -- Line One: Has 15 Characters -- Line Two: Has 14 Characters -- Line Three: Has 9 Characters -- DECLARE @Result AS VARCHAR(MAX) DECLARE @ST AS INT DECLARE @Words VARCHAR(MAX) DECLARE @tmpWord VARCHAR(MAX) DECLARE @t VARCHAR(MAX) DECLARE @I INT SET @Words = '' SET @Result = '' SET @I = 0 SET @ST = 0 SET @t = '' SET @tmpWord = '' -- Clean up sentence and remove any special characters found in it. -- This creates one long sustenance without the return characters. WHILE @I <= Len(@Sentence) BEGIN IF (SubString(@Sentence, @I, 1) in ('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '~', '`', ',', '.', '<', '>', '/', '?', '"', '[', ']', '{', '}', '\', '|', CHAR(13), CHAR(10), CHAR(39))) SET @Words = @Words + (CASE WHEN (SubString(@Sentence, @I, 1) = CHAR(10)) OR (SubString(@Sentence, @I, 1) = CHAR(13)) THEN ' ' ELSE SubString(@Sentence, @I, 1) END) SET @I = @I + 1 END SET @I = 0 -- This breaks down the sustenance into words and adds them -- back together based on the position you passed and the -- Segment length. It will include the punctuation as part of the -- word if there's no space between the word and the punctuation. -- Basically it's space delimited. -- -- PLEASE NOTE: If the last word in the sentence segment -- is greater that the length specified, it -- will include it! WHILE(@I < LEN(@Words)+1) BEGIN SELECT @t = SUBSTRING(@words,@I,1) IF(@t != ' ') BEGIN SET @tmpWord = @tmpWord + @t END ELSE BEGIN IF @tmpWord IS NOT NULL SET @Result = @Result + @tmpWord + ' ' IF @I >= @Pos1 BEGIN SET @Result = (CASE WHEN @ST = 0 THEN '' ELSE @Result END) SET @ST = 1 END IF LEN(@Result) >= @Len AND @I >= @Pos1 SET @I = 999999999 SET @tmpWord = '' END SET @I = @I + 1 SET @t = '' END IF @tmpWord IS NOT NULL AND @I < 999999999 AND @I >= @Pos1 SET @Result = @Result + @tmpWord IF @ST = 0 SET @Result = '' RETURN(@Result) END

Viewing all articles
Browse latest Browse all 6

Trending Articles