3
我正在使用拆分字符串方法,我已经将字符串拆分成固定长度的块,但问题是,它打破了这个词,我想保持完整单词和“空格”字符的基础上,拆分。在SQL Server 2008中将字符串拆分成固定长度的部分
下面我也附带了测试结果的功能。 请引导或指导我如何使用。
/*
Select dbo.SplitFixedLengthString('This is me , I am going to split this string in such a way that it will not break any word, rather it keeps word',16)
*/
CREATE FUNCTION [dbo].[SplitFixedLengthString]
(
@string NVARCHAR(MAX) ,
@stringlength INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @tempStr NVARCHAR(MAX)
DECLARE @finalString NVARCHAR(MAX)
IF LEN(@string) > 0
AND @stringlength > 0
BEGIN
SELECT @tempStr = ''
DECLARE @i INT
SET @i = 1
WHILE @i <= LEN(@string)
BEGIN
SELECT @tempStr = @tempStr + SUBSTRING(@string, @i,@stringlength) + (CHAR(13) + CHAR(10))
SET @i = @i + @stringlength
END
END
RETURN @tempStr
END
测试结果:
This is me , I a
m going to split
this string in
such a way that
it will not brea
k any word, rath
er it keeps word
(1行(S)的影响) 我们可以看到线路输出中的一个分裂的词 “是” 像明智的4号线5。
请给我解决方案。
这里是我的努力:
--DECLARE @x VARCHAR(32) = 'xyzxyzyyythgetdghydgsh j';
--SELECT LEN(@x) - CHARINDEX(' ', REVERSE(@x)) + 1;
DECLARE @string NVARCHAR(MAX)
SELECT @string = 'This is me , I am going to split this string in such a way that it will not break any word, rather it keeps word'
--SELECT @string = 'This is me,I am going to Trim off things in such a way that it will add newline having '
DECLARE @stringlength INT
SELECT @stringlength = 11
BEGIN
DECLARE @tempStr NVARCHAR(MAX)
DECLARE @finalString NVARCHAR(MAX) = ''
IF LEN(@string) > 0
AND @stringlength > 0
BEGIN
SELECT @tempStr = ''
DECLARE @start_index INT = 1
DECLARE @last_index INT = 0
DECLARE @lastindex INT = LEN(@string)-1
WHILE @start_index < @lastindex
BEGIN
SELECT @tempStr = SUBSTRING(@string, @start_index,@stringlength)
IF RIGHT(@tempStr,1) = ' '
BEGIN
SET @finalString = @finalString + @tempStr + (CHAR(13) + CHAR(10))
SET @start_index = @start_index + @stringlength
END
ELSE
BEGIN
--SELECT @last_index = LEN(@tempStr) - CHARINDEX(' ', REVERSE(@tempStr)) + 1;
SELECT @last_index = LEN(@tempStr) - CHARINDEX(' ',REVERSE(@tempStr))
IF @last_index = 0
BEGIN
SET @finalString = @finalString + LTRIM(SUBSTRING(@tempStr, 1,LEN(@tempStr)+1))+ (CHAR(13) + CHAR(10))
SET @start_index = @start_index + LEN(@tempStr)
END
ELSE
BEGIN
SET @finalString = @finalString + LTRIM(SUBSTRING(@tempStr, 1,@last_index +1))+ (CHAR(13) + CHAR(10))
SET @start_index = @start_index + @last_index
END
END
IF @start_index + @stringlength >= @lastindex
BEGIN
SET @finalString = @finalString + LTRIM(SUBSTRING(@string, @start_index,(@lastindex - @start_index)+1))+ (CHAR(13) + CHAR(10))
SET @start_index = @start_index + (@lastindex - @start_index)
END
END
END
SELECT @finalString
END
这里是输出
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is me
, I am
going to
split
this
string in
such a way
that it
will not
break any
word,
rather it
keeps wor
(1 row(s) affected)
它是足够的返回类型为表,而不是固定长度和重叠的字符串? – 2015-03-02 13:04:39
不,我需要在字符串中容纳,以显示在水晶报告中,对于某些语言,我必须手动添加换行,自动增长不起作用。我在下面附加了我的工作。如果可行的话,请帮助它 – DareDevil 2015-03-03 04:56:42