Re: using wildcards [ ] in REPLACE or CHARINDEX functions



On Fri, 11 May 2007 08:15:00 -0700, Joe Sich wrote:

I'm basically trying to replace non alpha characters of values in a couple
columns, but would prefer to create a ud-function or have a function in my
view do this for me rather than have to manually update every non-alpha
character.

I'm trying to do something like this:

REPLACE(Col001,[^ABCD...XYZ],' ')

In my mind, it reads replace any character in Col001 outside of the range of
A-Z with a space. But apparently I can only use wildcards with a LIKE? Did
I just answer my own question?

Anyone have any ideas how to accomplish this without explicitly stating all
non-alpha characters in an update query?

Thanks,
Joe

Hi Joe,

Unfortunately, that's not possible. SQL Server's string manipulation
functions are somewhat limited; this is one of the things they lack.

The fastest, though clumsy to write and maintain, is probably a
humongous nested REPLACE:

REPLACE(REPLACE(...(REPLACE(Col001, '!', ''), '@', ''), ...), '#', '')

Cleaner, but slower, would be to code a userdefined function that uses a
loop like this:

SET @pos = PATINDEX('%[^A-Z]%', UPPER(@String));
WHILE @pos > 0
BEGIN;
SET @String = REPLACE(@String, SUBSTRING(@String, @pos, 1), '');
SET @pos = PATINDEX('%[^A-Z]%', UPPER(@String));
END;


If you're using SQL Server 2005, an alternative might be to create a CLR
userdefined function that uses existing string manipulation functions of
the .Net framework to remove non alpha characters from the string. This
will definitely be faster than the T-SQL version with a loop and might
even be faster than the nested REPLACE maintenance nightmare.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
.



Relevant Pages

  • Re: Conversion error
    ... text (character strings) before importing them into SQL ... I have defined all columns in the SQL Server ... all values properly to varchar. ... money data. ...
    (microsoft.public.sqlserver.dts)
  • RE: strange problem with sql
    ... The difference in the character _ and W is just one bit. ... There are other letters that are getting mistranslated: ... My onlly guess now is that there is a memory ... > that at one occassion the SQL Server executed was changed on one ...
    (microsoft.public.data.ado)
  • Re: BULK COPY changes row order importing Text file
    ... Error = [ODBC SQL Server Driver]Invalid character value for cast ... BCP copy in failed ...
    (microsoft.public.sqlserver.programming)
  • RE: strange problem with sql
    ... The difference in the character _ and W is just one bit. ... There are other letters that are getting mistranslated: ... My onlly guess now is that there is a memory ... > that at one occassion the SQL Server executed was changed on one ...
    (microsoft.public.sqlserver.programming)
  • Re: Newbie performance/design question
    ... > character. ... just need to speed up this particular query, ... Pro SQL Server 2000 Database Design - ... > My first thought is to create a new field composed of the above,> concatenated together and separated by perhaps a '|' vertical bar ...
    (microsoft.public.sqlserver.programming)