Saturday, July 4, 2020

How to insert a string value into column with an single quote (apostrophe )

While tried to insert string value having single quote (apostrophe) character in sql server, it will not allow you to insert. it will throw error below.

Please follow below steps to resolve the issue.

Step 1: Create table with two columns

USE tempdb
GO
CREATE TABLE single_quote
(
  [ID] INT,
  [Name] VARCHAR(50)
)
GO

Step 2: Try to insert string with single quote value

INSERT INTO single_quote VALUES (1,'o'reilly books')
GO

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'reilly'.
Msg 105, Level 15, State 1, Line 10
Unclosed quotation mark after the character string ')
GO
'.

Step 3: Replace Single quote with double single quote

INSERT INTO single_quote VALUES (1,'o''reilly books')
GO

Step 4: Check the value

select * from single_quote

ID Name
1 o'reilly books

Now you can see the value got inserted correctly.

Watch the Video Here:


No comments:

Post a Comment