Frage

Possible Duplicate:
Replace single quote in sql server

So I'm trying to use the following code:

UPDATE TableName
SET FieldName = REPLACE(FieldName, 'SearchFor', 'ReplaceWith');

It works wonderfully for what I need to do, except for the fact that what I need to search for is "valid driver's license". That apostrophe doesn't seem to agree with the code. I tried doing this:

'valid driver''s license'

...but that doesn't seem to work either. I'm getting the following error:

Argument data type text is invalid for argument 1 of replace function.

If anyone has dealt with this before, I would love some help! This would save me so much time, rather than updating each record by hand. -__-

Ellie

War es hilfreich?

Lösung

The error tells you exactly what the problem is. FieldName is a column of type text, which doesn't work with the REPLACE function. Try casting the first param as VARCHAR(MAX) first, and it should work. IE:

UPDATE TableName
SET FieldName = REPLACE(CAST(FieldName AS VARCHAR(MAX)), 'SearchFor', 'ReplaceWith');

Andere Tipps

That's funny. I just ran into this exact thing a few minutes ago. It turns out I had to change this:

UPDATE TableName SET FieldName = REPLACE(FieldName, 'SearchFor', 'ReplaceWith');

to this: UPDATE TableName SET FieldName = REPLACE(cast(FieldName as varchar(5000)), 'SearchFor', 'ReplaceWith');

You must escape the single quote with a slash.

UPDATE TableName
SET FieldName = REPLACE(FieldName, 'Isn\'t', 'Is not');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top