Performing a case-sensitive search and replace in SQL 2000/2005 « No Free Time

Performing a case-sensitive search and replace in SQL 2000/2005 « No Free Time

If you have to be careful to maintian case used, i.e: ‘Shipping’ had to change to ‘Delivery’, but ‘shipping’ had to change to ‘delivery’.

You DO have to specify a collation method declareing it after the column name you’re interested in. Here’s an example:

update StringResource
set ConfigValue = replace(ConfigValue COLLATE Latin1_General_BIN, 'Shipping', 'Delivery')
from StringResource
where charindex('Shipping', configvalue COLLATE Latin1_General_BIN) > 0

update StringResource
set ConfigValue = replace(ConfigValue COLLATE Latin1_General_BIN, 'shipping', 'delivery')
from StringResource
where charindex('shipping', configvalue COLLATE Latin1_General_BIN) > 0

This query replaces the word ‘Shipping’ with ‘Delivery’ in the ConfigValue column in the StringResource table. There are two seperate statements, one for each case I’m replacing, because I need to specify the casing I’m concerned with explicitly. The important part of the collation type is the _BIN part, which specifies that I want to compare strings as binary data.


how to discover the collation for a database:
SELECT DATABASEPROPERTYEX('{database name}', 'Collation') SQLCollation;

and then, to know if your collation is case sensitive

SELECT *FROM fn_helpcollations()

Comments

Popular posts from this blog

Cannot fetch a row using a bookmark from OLE DB provider 'OraOLEDB.Oracle

Manual de alarma Chevrolet

http://www.colombiasoyyo.org