Amee
Amee

Reputation: 347

Single and double quotes in SQL Server

I want to know exact reason why this error occur playing with quotes.

INSERT INTO table_check(name) VALUES('hello'hi') -- ERROR
INSERT INTO table_check(name) VALUES('hello''hi') -- RESULT:- hello'hi
INSERT INTO table_check(name) VALUES('hello'''hi') --ERROR
INSERT INTO table_check(name) VALUES('hello''''hi') --RESULT:- hello''hi
INSERT INTO table_check(name) VALUES('hello'''''hi') --ERROR
INSERT INTO table_check(name) VALUES('hello''''''hi') --RESULT:- hello'''hi

Upvotes: 1

Views: 2190

Answers (1)

Aj.na
Aj.na

Reputation: 283

Single Quotes are Escaped by Doubling Them up.So whenever even number of Quotes are present, then we get the Result.

To Know The Behavior of Single Quotes Try to Run This Below Code:

Select '','''','''''','''''''',''''''''''

So,Single Quotes Should be Even Number Else We get error like:Unclosed quotation mark after the character string ') -- ERROR

Upvotes: 5

Related Questions