简体   繁体   中英

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. 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

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).

Otherwise, a gbn has mentioned, the only native TSQL operation available is a whole bunch of REPLACE s.

Here is function that will strip out special characters using ASCII character range. Caution: Make sure you test it and are happy with CPU usage before implementing it in a high volumne product environment.

This function modified from source-code.biz/snippets/mssql/1.htm by Christian d'Heureuse

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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