Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to insert text having ' (apostrophe) into a SQL table

While I was trying the following SQL command , I got sql error.

INSERT INTO exampleTbl VALUES('he doesn't work for me') 

where doesn't contain the apostrophe.

What is the way to insert text having ' (apostrophe) into a SQL table.

like image 537
Nazmul Avatar asked Dec 01 '11 05:12

Nazmul


People also ask

How do I insert an apostrophe in SQL database?

The short answer is to use two single quotes - '' - in order for an SQL database to store the value as ' .

How do you escape an apostrophe in SQL insert?

The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.

How do I find an apostrophe in a string in SQL?

You want to use a string insteaed. To put an apostrophe in a string literal you use double apostrophes. Show activity on this post. String sql="select lastname from employee where FirstName like '%"+firstName.


2 Answers

In SQL, the way to do this is to double the apostrophe:

'he doesn''t work for me' 

If you are doing this programmatically, you should use an API that accepts parameters and escapes them for you, like prepared statements or similar, rather that escaping and using string concatenation to assemble a query.

like image 152
Laurence Gonsalves Avatar answered Sep 19 '22 22:09

Laurence Gonsalves


INSERT INTO exampleTbl VALUES('he doesn''t work for me') 

If you're adding a record through ASP.NET, you can use the SqlParameter object to pass in values so you don't have to worry about the apostrophe's that users enter in.

like image 40
Ed B Avatar answered Sep 18 '22 22:09

Ed B