Groups | Blog | Home
all groups > sql server programming > july 2007 >

sql server programming : special character


AHartman
7/26/2007 11:12:47 PM
When building user ID's that get loaded into a Table I sometimes run across
names like O'Learah.

What is the sql statement that will take field called Userid that contains:

O'Learah

and create

OLearah removing the (')

Thanks.
Tom Cooper
7/26/2007 11:19:43 PM
Replace (<your column or variable>, '''', '')
E.G.,
Declare @Test varchar(20)
Select @Test = 'O''Learah'
Select @Test, Replace(@Test, '''', '')

Tom

[quoted text, click to view]

Plamen Ratchev
7/26/2007 11:24:32 PM
You can use REPLACE, like this:

DECLARE @foo NVARCHAR(50);

SET @foo = N'O''Learah';

SELECT REPLACE(@foo, '''', '');

HTH,

Plamen Ratchev
http://www.SQLStudio.com

AHartman
7/26/2007 11:27:17 PM

Thanks for the quick response....

Will the replace statement get confused if the character your removing is a
single quote not double quote?


[quoted text, click to view]
Plamen Ratchev
7/26/2007 11:58:34 PM
It is actually a single quote that gets replaced. Since the single quote is
used to enclose string literals, you have to use two single quotes to
specify a single quote to be replaced. If you prefer you can use CHAR with
the ASCII code of the single quote (39), like this:

SELECT REPLACE(@foo, CHAR(39), '');

HTH,

Plamen Ratchev
http://www.SQLStudio.com

AHartman
7/27/2007 6:05:36 AM
Thanks!!!!


[quoted text, click to view]
AddThis Social Bookmark Button