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
 single | double quotes

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-10-25 : 04:29:50
Hi,

i was meaning to write this for quite a long time.
in following case i was using bulk export to file:


declare @file varchar(50)
declare @cmd varchar(2000)

set @file = 'C:\adventureWorks_emails'
set @cmd
= 'bcp "select EmailAddress from Person.Contact where ContactID between 10 and 15" queryout "'
SET @cmd
= @cmd + @file + '" -t -T -c'

exec master..xp_cmdshell @cmd


if i would be using in select statement string selection, query would report error, eg.:

..
select EmailAddress from Person.Contact
where EmailAddress like '%@ad%'
..


due to single quote. any solutions?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-25 : 04:32:53
put '' instead of ' to escape ' quotes. so it will be like
select EmailAddress from Person.Contact 
where EmailAddress like ''%@ad%''

inside the @cmd variable.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-25 : 04:34:01
Also see this to understand how single quote works

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-10-26 : 02:56:08
Great. Thanks for posting URL to Madhivanan article.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-26 : 07:00:55
quote:
Originally posted by slimt_slimt

Great. Thanks for posting URL to Madhivanan article.


Welcome
I found it very useful.Thats why thought of posting it.
Go to Top of Page
   

- Advertisement -