在第n个字符后附加任何特殊字符而不会破坏SQL Server中的单词

时间:2018-05-31 02:37:18

标签: sql sql-server sql-server-2008 sql-server-2012

我的查询在结果集中返回超过10万行,例如。

  ID   Description 
   1   This site uses cookies to deliver our services and to show you relevantads and job listings. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and term 
   2   RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access .The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
    3  ................
    4  .............
    5
    and so on 

我已使用此功能在第n个位置后添加任何特殊内容:

CREATE FUNCTION dbo.fn_BreakString (@STR VARCHAR(MAX), @CHARLEN INT)
RETURNS VARCHAR(MAX)
AS
BEGIN
    DECLARE @NEW_STR VARCHAR(MAX), @POS INT, @LEN INT

    SET @NEW_STR = ''
    SET @POS = 1
    SET @LEN = LEN(@STR)

    WHILE @POS < @LEN
    BEGIN
        IF @POS = 1
        BEGIN
            SET @NEW_STR = SUBSTRING(@STR, @POS, @CHARLEN)
        END
        ELSE
        BEGIN
            SET @NEW_STR = @NEW_STR + @#$ + SUBSTRING(@STR, @POS, @CHARLEN)
        END

        SET @POS = @POS + @CHARLEN
    END

    RETURN @NEW_STR
END

但是我得到这样的输出:

select 
    id, dbo.fn_BreakString(description, 10) desc
from 
    tablea 

输出:

ID   Description 
 1   This site u@#$ses cookie@#$s to deliver@#$our servi@#$ces and to show you @#$relevantads and job listings.including the Stack Overflow Network, is subject to these policies and term 
 2   RDBMS is t@#$he basis fo@#$r SQL, and @#$for all modern database systems such as MS SQL Server
 3  ................
 4  .............
 5
 and so on 

我试图在第n个位置附加特殊而不破坏它可以在第n个位置之前或之后就可以了。

期望的输出:

ID   Description 
 1   This site@#$uses cookies@#$ to deliver our@#$ services and to show you relevant ads and job listings. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms
 2   RDBMS is@#$the basis@#$for SQL, and @#$for all modern database systems such as MS SQL Server
 3  ................
 4  .............
 5
 and so on 

我已经在脚本下面尝试了它,但有时会进入无限循环

DECLARE @NEW_STR NVARCHAR(MAX), @POS INT, @LEN INT, @End INT, @NEW_STRend NVARCHAR(MAX) = ''
SET @NEW_STR = ''
SET @POS=1
SET @LEN = LEN(@STR)

SELECT @LEN

WHILE @POS < @LEN
BEGIN
    IF @POS = 1
    BEGIN
        PRINT @pos
        SET @End = LEN(LEFT(@Str, @CHARLEN)) - CHARINDEX(' ', REVERSE(LEFT(@Str, @CHARLEN)))

        PRINT @End
        SET @NEW_STR = SUBSTRING(@STR, @POS,@End )
    END
    ELSE
    BEGIN
        SET @End = LEN(LEFT(@Str, @CHARLEN)) - CHARINDEX(' ', REVERSE(LEFT(@Str, @CHARLEN)))
        PRINT @End

        SET @NEW_STR = @NEW_STR +@#$+ SUBSTRING(@STR, @POS+1,@End )
    END

    --select @pos ,@charlen
    SET @POS = @POS + @CHARLEN
END

SELECT @NEW_STR

2 个答案:

答案 0 :(得分:2)

您需要从要放置特殊字符的位置找到下一个空格('')的索引,然后在那里添加字符。

DECLARE @nextPos INT

--then in your if & else
SET @nextPost = CHARINDEX(' ', @STR, @POS)

--then use that in place of @POS in your SUBSTRINGs

答案 1 :(得分:0)

Declare @STR nvarchar(max)='String to add special character',
 @CHARLEN int=5 --postion to add character
  DECLARE @NEW_STR nvarchar(max),
          @POS int,
          @LEN int
  SET @NEW_STR = ''
  SET @POS = 1
  SET @LEN = LEN(@STR)

WHILE LEN(@STR)>0
BEGIN   

    IF CHARINDEX(' ',@STR,@CHARLEN) >0 
    BEGIN

      SET @NEW_STR = @NEW_STR+'@!#'+SUBSTRING(@STR, 1, case when charindex(' ',@str,@CHARLEN)-1 <0 then len(@str)else charindex(' ',@str,@CHARLEN)-1 end )
      SELECT @STR= SUBSTRING(@STR,CASE WHEN LEN(SUBSTRING(@STR, 1, CHARINDEX(' ',@STR,@CHARLEN)))+1>LEN(@STR) THEN 1 ELSE LEN(SUBSTRING(@STR, 1, CHARINDEX(' ',@STR,@CHARLEN)))+1 END ,LEN(@STR))

    END
    ELSE IF CHARINDEX(' ',@STR,@CHARLEN) =0
    BEGIN

        SET @NEW_STR=@NEW_STR+SUBSTRING(@STR,1,LEN(@STR))
        --PRINT @NEW_STR
        SET @STR=''
    END
    ELSE 
    BEGIN 

     SET @STR=''

    END 
end 
  select  @NEW_STR