﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / single quote in sql statement / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 03:25:52 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: single quote in sql statement</title><link>http://www.sqlservercentral.com/Forums/Topic1396846-392-1.aspx</link><description>Thanks, that is helpful!</description><pubDate>Mon, 17 Dec 2012 12:56:41 GMT</pubDate><dc:creator>sqlfriends</dc:creator></item><item><title>RE: single quote in sql statement</title><link>http://www.sqlservercentral.com/Forums/Topic1396846-392-1.aspx</link><description>Since this is a backup you are trying to run, here is another option:[code="sql"]declare @dbname varchar(128),        @filedate varchar(64);declare @diskfile varchar(256) = 'H:\Preupgrade\Backups\' + @dbname + '_' + @filedate;declare @filename varchar(256) = @dbname + @filedate;declare @SQLCmd varchar(max) ='BACKUP DATABASE @dbnameTO DISK = @diskfileWITH NOFORMAT,     NAME = @filename,     SKIP,     REWIND,     NOUNLOAD,     STATS = 10;';declare @Params @varchar(max) = '@dbname varchar(128), @diskfile varchar(64), @filename varchar(256)';exec sp_executesql    @SQLCmd,    @Params,    @dbname = @dbname,    @diskfile = @diskfile,    @filename = @filename;[/code]</description><pubDate>Sat, 15 Dec 2012 13:48:33 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: single quote in sql statement</title><link>http://www.sqlservercentral.com/Forums/Topic1396846-392-1.aspx</link><description>Thank you both for this, this makes much clearer to understand!</description><pubDate>Sat, 15 Dec 2012 12:04:35 GMT</pubDate><dc:creator>sqlfriends</dc:creator></item><item><title>RE: single quote in sql statement</title><link>http://www.sqlservercentral.com/Forums/Topic1396846-392-1.aspx</link><description>If the no. of quotes are still confusing for you in the solution provided by Lynn......then you can also use the CHAR() function in sql server to apply quotes by using the ASCII value of a single quote(39) as shown below : [code="sql"]declare @dbname varchar(128) = 'TestDB',        @filedate varchar(64) = '20121214210000',        @sqlcmd varchar(max);SET @sqlcmd = 'BACKUP DATABASE ' + @dbname + ' TO DISK = ' + CHAR(39) + 'H:\Preupgrade\               Backups\' + @dbname + '_' + @filedate + CHAR(39) + ' WITH NOFORMAT, NOINIT, ' +               'NAME = ' + CHAR(39) + @dbname + @filedate + CHAR(39) + ', SKIP, REWIND, NOUNLOAD,                STATS = 10';print @sqlcmd;[/code]</description><pubDate>Fri, 14 Dec 2012 21:52:27 GMT</pubDate><dc:creator>vinu512</dc:creator></item><item><title>RE: single quote in sql statement</title><link>http://www.sqlservercentral.com/Forums/Topic1396846-392-1.aspx</link><description>This is what you are trying to accomplish:[code="sql"]declare @dbname varchar(128) = 'TestDB',        @filedate varchar(64) = '20121214210000',        @sqlcmd varchar(max);SET @sqlcmd = 'BACKUP DATABASE ' + @dbname + ' TO DISK = ''H:\Preupgrade\ Backups\' +              @dbname + '_' + @filedate + '''' + ' WITH NOFORMAT, NOINIT, ' +              'NAME = ' + '''' + @dbname + @filedate + '''' + ', SKIP, REWIND, NOUNLOAD, STATS = 10';print @sqlcmd;[/code]</description><pubDate>Fri, 14 Dec 2012 21:05:37 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>single quote in sql statement</title><link>http://www.sqlservercentral.com/Forums/Topic1396846-392-1.aspx</link><description>I would like to set a variable that is a sql statement, but the syntax about the quotes is so confusing to me, I cannot make it right:What I have is this:  SET @sqlcmd = 'BACKUP DATABASE '+@dbname+' TO DISK = ''H:\Preupgrade\ Backups\'+@dbname+'_'+@filedate +' WITH NOFORMAT, NOINIT,' +      ' NAME = '''+@dbname+@filedate+''', SKIP, REWIND, NOUNLOAD,  STATS = 10'then   EXEC sp_executesql @sqlcmdHow can I use single quotes in statement like above?Thanks</description><pubDate>Fri, 14 Dec 2012 18:07:26 GMT</pubDate><dc:creator>sqlfriends</dc:creator></item></channel></rss>