Click here to Skip to main content
16,004,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I insert string value including single quote into a string type table column?
Posted

Use '' (two single quotes). Ex: want to save Peter's then it should be Peter''s.
 
Share this answer
 
please see this
C#
Put quite simply:

SELECT 'This is Ashok''s Pen.';

So inside the string, replace each single quote with two of them.

Or:

SELECT 'This is Ashok\'s Pen.'
 
Share this answer
 
v2
Comments
Thanks7872 18-Jul-14 2:23am    
This link is pointing to this question only. Update it.
Hi,

It is never a good practice to insert Single Quote " ' " with your data in database as it leads to SQL Injections.

Anyway if you still want to do it....can check below

SQL
insert into your_Table (your_col) values ('he''s')




As per your comments check below


SQL
DECLARE @strVar VARCHAR(100)
SET @strVar='hi''s'
print(@strVar)





Hope this will be helpful to you.





Cheers.
 
Share this answer
 
v2
Comments
rhl4569 18-Jul-14 2:37am    
thanks sir. but if i have string variable then how this applys to that ?
Magic Wonder 18-Jul-14 3:26am    
check above solution.
use \ before sting value where you want to insert single quote

i.e. string name = " ABC \'XYZ\'";

sample example

C#
string connectionString; 
            connectionString   =  "Data Source=USER\\SQLEXPRESS;Initial Catalog=db;Integrated Security=SSPI;";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO [Employee]([Name],[EmployeeDepartment])VALUES (@Name,@EmployeeDepartment) ", conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@Name", SqlDbType.VarChar, 500).Value = "ABC \'XYZ\'";
            cmd.Parameters.Add("@EmployeeDepartment", SqlDbType.VarChar, 50).Value = "Dept";
            cmd.ExecuteNonQuery();
            conn.Close();



SQL Query that you can fire from SSMS

SQL
INSERT INTO [Employee]
           ([Name]
           ,[EmployeeDepartment])
     VALUES
           ('ABC ''XYZ'''
           ,'dept'
           )
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900