SQL Server How to add a string with (') in insert in to statement in SQL [duplicate]

vatpfxk5  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(143)

This question already has answers here:

Way to insert text having ' (apostrophe) into a SQL table (8 answers)
Closed 8 days ago.

I am trying to manually add a item into my table in SQL Server by using insert into statements, but I get an error.

Normally a string is added using a single apostrophe in front and back of the string in SQL Server, but I am adding a value which has an apostrophe in between (like can't ), how to add this in the T-SQL insert into statement?

I did try 3 different methods to insert but still failed

insert into orders (6, 'microsoft surface pro', 'IDHGHRTUJ'''123456', 1, 8)
insert into orders (6, 'microsoft surface pro', 'IDHGHRTUJ'123456', 1, 8)
insert into orders (6, 'microsoft surface pro', "IDHGHRTUJ'123456", 1, 8)

I need output in this of the string with the apostrophe in iot

k5hmc34c

k5hmc34c1#

You can insert single quote in database by using double single quote while providing values as shown below:

create table orders (OrderId int, ProductName varchar(50), ProductDescription varchar(50), CatId int, GroupId int)
insert into orders values (6, 'microsoft surface pro', 'IDHGHRTUJ''123456', 1, 8)

select * from orders

Here is the output after insert

OrderId ProductName ProductDescription  CatId   GroupId
--------------------------------------------------------
6   microsoft surface pro   IDHGHRTUJ'123456    1   8

You can find the live demo here

prdp8dxp

prdp8dxp2#

Another way is to use CONCAT() within your insert statement.

create table #orders (OrderId int, ProductName varchar(50), ProductDescription varchar(50), CatId int, GroupId int)
 insert into #orders values (6, 'microsoft surface pro', CONCAT('IDHGHRTUJ','''','123456'), 1, 8)

Used a temp table #orders for testing

select * from #orders

相关问题