SQL Server – How to Handle Single Quote in String

How to Handle Single Quote in String

This is the common one-time problem (not for all ;-)) faced by a new DBA Developer, and even I did.

The solution to this problem is rather simple. Use an extra single quote, with the quote character occurring in the string.

See the example below:

— The Insert Satement below will give and error

INSERT INTO Employee VALUES (1003, ‘Patrick’, ‘O’Brain’)

Error Message

Msg 105, Unclosed quotation mark after the character string

— Correct Statement

INSERT INTO Employee VALUES (1003, ‘Patrick’, ‘O”Brain’)

— Similarly you can use it in WHERE clause also

SELECT * FROM Employee WHERE LastName=’O”Brain’

See also:

Function to split comma delimited String or List

How to handle single quote in sqlquery string?

Good Luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: