Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server Insert Data with Apostrophe/Single Quote in Database

Nov 28, 2012
Introduction

In this SQL Server article I will explain how to insert single quote or apostrophe data in database.

Description

In previous articles I explained While Loop Example in SQL Server, Find First and last day of current month in SQL, Convert Rows to Columns in SQL, Joins in SQL Server and many articles relating to SQL Server.  Now I will explain how to insert single quote or apostrophe data in SQL Server.

In one situation I got requirement like insert data with single quote or apostrophe in database whenever I tried to insert data with single quote that is throwing error like 

Unclosed quotation mark after the character string

Check below example here I created one temp table and trying to insert single quote data into that table 


DECLARE @temp TABLE (ID INT,Location varchar(120))
INSERT INTO @temp VALUES(1,'Near to Men's Hostel')
SELECT * FROM @temp
Whenever we run above query we will get error like “Unclosed quotation mark after the character string” because in SQL Server single quote or apostrophe is used for string delimiter. 

To insert single quote or apostrophe data in database we need to use two consecutive single quotes or apostrophes in data for that Check below Example 


DECLARE @temp TABLE (ID INT,Location varchar(120))
INSERT INTO @temp VALUES(1,'Near to Men''s Hostel')
SELECT * FROM @temp
Once we run above query we will get output like as shown below

Output

In this way we can insert single quote or apostrophe data in database using SQL Server.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

7 comments :

Anonymous said...

U r using two consecutive single in both Example

Suresh Dasari said...

Thanks for pointing my mistake i modified the post.

Anonymous said...

Good job Keep it up

Anonymous said...

Great Job Bro keep it up u rock

Umrah said...

Your post is very informative. but let me know how to insert data of contact form in database . Just insertions
you can contact form. I have these variables: 1- Name 2- Email 3- Phone 4- Comment . I am waiting for your reply.

Anonymous said...

how to enter a data with single quotes...

Unknown said...

how can i retrieve the same in a textbox in c#. while doing so it's giving me an error dangerous something something...

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.