Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to add special characters in insert query

Author  Topic 

pearlkin
Starting Member

3 Posts

Posted - 2011-06-03 : 08:30:47
Hi Forumians,

I have to execute the below query

insert into query
values
(
'to find ipaddress',

'exec master..xp_cmdshell 'ipconfig'',
'http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=148251',
getdate()
)

here i have ' character in between the 2 nd attribute. how to store this value exec master..xp_cmdshell 'ipconfig' in the query table.

i am getting d below error

Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near 'ipconfig'.

Note i have to enclose ipconfig word in single quote.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-03 : 08:44:44
You can escape a single quote with another single quote. So

insert into query
values
(
'to find ipaddress',

'exec master..xp_cmdshell ''ipconfig''',
'http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=148251',
getdate()
)
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-06-03 : 11:28:15
Another alternative is to use the CHAR function to replace single quotes. For example:
insert into query
values
(
'to find ipaddress',

'exec master..xp_cmdshell ' + CHAR(39) + 'ipconfig' + CHAR(39),
'http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=148251',
getdate()
)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-06-03 : 11:57:57
see

http://beyondrelational.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -