user11568901
user11568901

Reputation:

How can I replace a `'` in a string in SQL?

I have to replace special characters in SQL.

My problem is with ', because it is used for start and end of string in SQL.

I tried:

ID = REPLACE(ID, ''', '')
ID = REPLACE(ID, "'", "")

But both not worked. What should I do?

Upvotes: 1

Views: 87

Answers (2)

Programnik
Programnik

Reputation: 1555

Either Use the char function and ascii code:

ID = REPLACE(ID, char(39), '')

or double down on the single quotes:

ID = REPLACE(ID, '''', '')

Upvotes: 1

Arulkumar
Arulkumar

Reputation: 13237

ID = REPLACE(ID, '''', '') will work.

Demo with sample data:

DECLARE @ID AS VARCHAR(10) = 'Test''data';
SELECT @ID, REPLACE(@ID, '''', '')

it will remove the single quote from the given string.

Upvotes: 2

Related Questions