如何使用TSQL用短划线替换非单词字符?

时间:2014-12-14 15:50:55

标签: sql-server tsql

如何使用TSQL用~!@#$%^&*()_+}{][替换nvarchar(或varchar)字段中的字符-

5 个答案:

答案 0 :(得分:0)

您可以使用REPLACE功能。如果在某些情况下不起作用,请举例说明。

您正在寻找this code

-- Author: Christian d'Heureuse, www.source-code.biz
create function dbo.RemoveSpecialChars (@s varchar(256)) returns varchar(256)
   with schemabinding
begin
   if @s is null
      return null
   declare @s2 varchar(256)
   set @s2 = ''
   declare @l int
   set @l = len(@s)
   declare @p int
   set @p = 1
   while @p <= @l begin
      declare @c int
      set @c = ascii(substring(@s, @p, 1))
      if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122
         set @s2 = @s2 + char(@c)
      set @p = @p + 1
      end
   if len(@s2) = 0
      return null
   return @s2
   end

删除除0-9,a-z和A-Z之外的所有字符。此函数使用ASCII字符代码来确定必须删除的字符。

答案 1 :(得分:0)

您可以为此创建用户定义函数,如下所示

CREATE FUNCTION udf_ReplaceSpecialChar
(
@inputString VARCHAR(1000)
)
RETURNS VARCHAR(1000)
AS 
BEGIN
    DECLARE @outputString VARCHAR(1000),
            @LENGTH INT,
            @index INT,
            @char CHAR(1)
    SELECT @LENGTH = LEN(@inputString),
           @index = 1
    WHILE(@index <= @LENGTH)
    BEGIN
        SET @char = SUBSTRING(@inputString, @index, 1)
        IF((ASCII(@char) NOT BETWEEN 65 AND 90) AND (ASCII(@char) NOT BETWEEN 97 AND 122) AND (ASCII(@char) NOT BETWEEN 48 AND 57))
        BEGIN
            SELECT @inputString = REPLACE(@inputString, @char, '-')
        END
        SET @index = @index + 1
    END 
    SET @outputString = @inputString
    RETURN @outputString
END

SELECT dbo.udf_ReplaceSpecialChar('This()*& is%%@Sample**.>String')

或者您应该用' - '替换每个字符 像

SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE('This()*& is%%@Sample**.>String', ' ', '-'), '*', '-'), '@', '-'), '&', '-'), '(', '-'), ')', '-'), '.', '-'), '>', '-'), '%', '-')

答案 2 :(得分:0)

--another one variant    
----------------------------------------------------------------------------------------
    --better to keep such table in server, very usefull table, especially with indexes 
    DECLARE @Tally TABLE ( N INT ) 
    DECLARE  @i AS INT = 1
    WHILE @i != 1000 
        BEGIN
            INSERT  INTO @Tally
                    ( N )
            VALUES  ( @i )
            SET @i = @i + 1
        END
    ----------------------------------------------------------------------------------------
    DECLARE @String AS VARCHAR(1000) = 'This()*& is%%@ **.>another one //&^&*$variant'
    ----------------------------------------------------------------------------------------

    --using @tally - split, using like - remove not required, 'for xml ...' - combine into string 
    SELECT  REPLACE(( SELECT    LEFT(SUBSTRING(@String, n, 1000), 1)
                      FROM      @Tally AS T
                      WHERE     SUBSTRING(@String, n, 1000) != ''
                                AND LEFT(SUBSTRING(@String, n, 1000), 1) LIKE '[A-Za-z0-9 ]'
                    FOR
                      XML PATH('')
                    ), '&#X20;', ' ')

答案 3 :(得分:0)

    --another one variant    
    ------------------------------------------------------------------------------------
    --better to keep such table in server, very usefull table, especially with indexes 
      DECLARE @Tally TABLE ( N INT ) 
      DECLARE @i AS INT = 1
      WHILE @i != 1000 
        BEGIN
            INSERT  INTO @Tally
                    ( N )
            VALUES  ( @i )
            SET @i = @i + 1
        END
    ------------------------------------------------------------------------------------
      DECLARE @String VARCHAR(500) ,
        @B VARCHAR(500) = '' 
      SET @String = 'This()*& is%%@ **.>another one //&^&*$variant'

      SELECT    @B = @B + SUBSTRING(@String, t.N, 1)
      FROM      @Tally t
      WHERE     t.N <= DATALENGTH(@String)
                AND PATINDEX('[A-Za-z0-9 ]', SUBSTRING(@String, t.N, 1)) > 0

      SELECT    @B
--------------------------------------------------------------------------------
  

如果您希望像函数一样使用此方法:

     
      
  1. 使用一个字段PRIMARY KEY创建Tally表(1000行,从1开始,步骤1)
  2.   
  3. 使用以下代码创建功能
  4.         

    表格Tally对于分裂刺痛,干净的弦乐等非常有用,目前这是   使用fetch,xml等的最佳方法。

--------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[StringClean]( 
    @A VARCHAR(500)) 
RETURNS VARCHAR(500) 
AS 
BEGIN 
DECLARE @B VARCHAR(500)
SET @B = ''
SELECT  @B = @B + SUBSTRING(@A, t.N, 1)
FROM    dbo.Tally t
WHERE   t.N <= DATALENGTH(@A)
        AND PATINDEX('[A-Za-z0-9 ]', SUBSTRING(@A, t.N, 1)) > 0
RETURN @B
END
-------------------------------------------------------------------------------
SELECT  dbo.StringClean('This()*& is%%@ **.>another one //&^&*$variant')
-------------------------------------------------------------------------------

答案 4 :(得分:0)

    DECLARE @Tally TABLE ( N INT ) 
    DECLARE  @i AS INT = 1
    WHILE @i != 1000 
        BEGIN
            INSERT  INTO @Tally (N) VALUES  (@i)
            SET @i = @i + 1
        END
--------------------------------------------------------------
    DECLARE @String VARCHAR(500) 
    DECLARE @B VARCHAR(500) = '' 
    DECLARE @ReplacedChars VARCHAR(50) = '~!@#$%^&*()_+}{][<>/.'
    SET @String = 'This()*& is%%@ **.>another one //&^&*$variant'
    SELECT  @B = @B + CASE WHEN CHARINDEX(SUBSTRING(@String, t.N, 1), @ReplacedChars) > 0 THEN '-'
                           ELSE SUBSTRING(@String, t.N, 1) END
    FROM    @Tally t
    WHERE   t.N <= DATALENGTH(@String)
    SELECT  @B