简体   繁体   English

sql server 2000:TSQL特殊字符处理

[英]sql server 2000: TSQL special characters handling

Just using SQL Server 2000 built in features ONLY, what is the best way to handle special characters. 仅使用内置功能的SQL Server 2000,什么是处理特殊字符的最佳方法。 I am not sure if Regular Expression could be used by purely using built in features? 我不确定是否可以仅通过使用内置功能来使用正则表达式? I would like to search and replace the special characters in my queries. 我想搜索并替换查询中的特殊字符。

Thanks 谢谢

Nested replace 嵌套替换

REPLACE(REPLACE(REPLACE(value, '$', ''), '"', ''), ':', '')

Really, this isn't something t-sql is good at 真的,这不是t-sql擅长的

Although this may violate the definition of using "built-in features ONLY", since it relies on WSH, the function outlined in this posting is one way to get regexes in to SQL 2000, and could be extended to support replacement etc. While this isn't pure TSQL, it shouldn't require any new software or extensions on the server (although many DBAs would lock down the COM-scripting stored proc). 尽管这可能违反使用“ONLY内置功能”的定义,因为它依赖于WSH,在所列的功能此帖子是让正则表达式到SQL 2000的一种方式,并可以扩展到支持更换等,虽然这不是纯TSQL,它不应该在服务器上需要任何新软件或扩展(尽管许多DBA会锁定COM脚本存储的proc)。

Otherwise, a gbn has mentioned, the only native TSQL operation available is a whole bunch of REPLACE s. 否则,gbn会提到,唯一可用的本机TSQL操作是一堆REPLACE

Here is function that will strip out special characters using ASCII character range. 这是将使用ASCII字符范围去除特殊字符的功能。 Caution: Make sure you test it and are happy with CPU usage before implementing it in a high volumne product environment. 注意:在大批量产品环境中实施它之前,请确保对其进行测试并且对CPU使用情况感到满意。

This function modified from source-code.biz/snippets/mssql/1.htm by Christian d'Heureuse 此功能由Christian d'Heureuse从source-code.biz/snippets/mssql/1.htm修改

    CREATE FUNCTION dbo.RemoveSpecialChars (@s VARCHAR(256)) 
RETURNS VARCHAR(256)
   WITH SCHEMABINDING
BEGIN
   IF @s is null
      RETURN null

   DECLARE @s2 varchar(256)
   DECLARE @l int
   DECLARE @p int


   SET @s2 = ''
   SET @l = len(@s)

   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
            BEGIN
                  SET @s2 = @s2 + char(@c)
            END
            SET @p = @p + 1
      END


      IF LEN(@s2) = 0
      BEGIN
            RETURN null
      END     

      RETURN @s2

END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM