<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; How to Escape Single Quotes &#8211; Fix: Error: 105 Unclosed quotation mark after the character string &#8216;</title>
	<atom:link href="http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/</link>
	<description>SQL, SQL Server, MySQL, Big Data and NoSQL</description>
	<lastBuildDate>Wed, 19 Jun 2013 06:53:59 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Brendan</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-458042</link>
		<dc:creator><![CDATA[Brendan]]></dc:creator>
		<pubDate>Wed, 17 Apr 2013 16:06:21 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-458042</guid>
		<description><![CDATA[use four &#039; (&#039;&#039;&#039;&#039;)]]></description>
		<content:encoded><![CDATA[<p>use four &#8216; (&#8221;&#8221;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #017 &#124; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-426021</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #017 &#124; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 23 Feb 2013 01:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-426021</guid>
		<description><![CDATA[[...] How to Escape Single Quotes In this example there is a direct script which explains how to escape single quotes in SQL Server. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] How to Escape Single Quotes In this example there is a direct script which explains how to escape single quotes in SQL Server. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ajay</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-389785</link>
		<dc:creator><![CDATA[Ajay]]></dc:creator>
		<pubDate>Sat, 08 Dec 2012 19:15:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-389785</guid>
		<description><![CDATA[can any one tell me ,what to do when we are getting data from web server with  @ &#039; @ ,i mean single quote.i have to load that data in my sql database.please tell the solution.
Regards 
Ajay Kohar]]></description>
		<content:encoded><![CDATA[<p>can any one tell me ,what to do when we are getting data from web server with  @ &#8216; @ ,i mean single quote.i have to load that data in my sql database.please tell the solution.<br />
Regards<br />
Ajay Kohar</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vignesh</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-342777</link>
		<dc:creator><![CDATA[Vignesh]]></dc:creator>
		<pubDate>Tue, 11 Sep 2012 08:56:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-342777</guid>
		<description><![CDATA[i need sql insert stament without stored procedure to insert dynamic values from textbox which contains of (Apostrophe,Double quotes)! Simple example plz]]></description>
		<content:encoded><![CDATA[<p>i need sql insert stament without stored procedure to insert dynamic values from textbox which contains of (Apostrophe,Double quotes)! Simple example plz</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kapil sharma</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-339479</link>
		<dc:creator><![CDATA[kapil sharma]]></dc:creator>
		<pubDate>Fri, 31 Aug 2012 07:00:59 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-339479</guid>
		<description><![CDATA[Hello Sir i am using a query in which i have generated a string from Comma separate and when i use it in &quot;select id from tblName where id in(@str) &quot; here id is integer and i have @str as nvarchar its not giving result as it should.. please help me ... Thanks in advance.......]]></description>
		<content:encoded><![CDATA[<p>Hello Sir i am using a query in which i have generated a string from Comma separate and when i use it in &#8220;select id from tblName where id in(@str) &#8221; here id is integer and i have @str as nvarchar its not giving result as it should.. please help me &#8230; Thanks in advance&#8230;&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wes</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-322616</link>
		<dc:creator><![CDATA[Wes]]></dc:creator>
		<pubDate>Tue, 31 Jul 2012 21:37:07 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-322616</guid>
		<description><![CDATA[Martin,

How&#039;s it feel up on your ivory tower?

Thanks for sharing your solution for the we, your inferiors. It&#039;s a good solution and just saved me some time.

But WTF do you mean by scolding us rubes with &quot;Learn how to learn?&quot; Are you going to tell us that you&#039;ve never posted a perfectly good question on one forum or another?

One way to learn is to ask. That&#039;s what the OP was doing.]]></description>
		<content:encoded><![CDATA[<p>Martin,</p>
<p>How&#8217;s it feel up on your ivory tower?</p>
<p>Thanks for sharing your solution for the we, your inferiors. It&#8217;s a good solution and just saved me some time.</p>
<p>But WTF do you mean by scolding us rubes with &#8220;Learn how to learn?&#8221; Are you going to tell us that you&#8217;ve never posted a perfectly good question on one forum or another?</p>
<p>One way to learn is to ask. That&#8217;s what the OP was doing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mr Right</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-309624</link>
		<dc:creator><![CDATA[Mr Right]]></dc:creator>
		<pubDate>Thu, 05 Jul 2012 12:14:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-309624</guid>
		<description><![CDATA[hi guys plz help me in making a function in T-sql through which i can change the string like
Double Quto(&quot;) into single quto(&#039;) by using loop...
like Microsoft&quot;s into Microsoft&#039;s
Microsoft&quot;&quot; into Microsoft&#039;s
and Microsoft&#039;s shuld remain Microsoft&#039;s]]></description>
		<content:encoded><![CDATA[<p>hi guys plz help me in making a function in T-sql through which i can change the string like<br />
Double Quto(&#8220;) into single quto(&#8216;) by using loop&#8230;<br />
like Microsoft&#8221;s into Microsoft&#8217;s<br />
Microsoft&#8221;" into Microsoft&#8217;s<br />
and Microsoft&#8217;s shuld remain Microsoft&#8217;s</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jgsjd</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-309618</link>
		<dc:creator><![CDATA[jgsjd]]></dc:creator>
		<pubDate>Thu, 05 Jul 2012 12:05:16 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-309618</guid>
		<description><![CDATA[how can we change &quot;,&quot;&quot; into single &#039;]]></description>
		<content:encoded><![CDATA[<p>how can we change &#8220;,&#8221;" into single &#8216;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: techiechick</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-301133</link>
		<dc:creator><![CDATA[techiechick]]></dc:creator>
		<pubDate>Thu, 14 Jun 2012 10:40:42 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-301133</guid>
		<description><![CDATA[OK, I&#039;ve found a fairly inelegant solution... there was an escaping &quot;\&quot; in there, so my search string was &quot;O\&#039;&#039;Connor&quot;. Have now changed the code to:
			$querystr .= &quot;AND UPPER(Authors) COLLATE SQL_Latin1_General_Cp1_CI_AS LIKE &#039;%&quot;.strtoupper(ereg_replace(&quot;&#039;&quot;,&quot;_&quot;,stripslashes($Authors))).&quot;%&#039;  &quot;;

...and this appears to work - the underscore allowing for the apostrophe, and the % working for...everything else.

Thanks for providing me a forum to think through this in public ;-)]]></description>
		<content:encoded><![CDATA[<p>OK, I&#8217;ve found a fairly inelegant solution&#8230; there was an escaping &#8220;\&#8221; in there, so my search string was &#8220;O\&#8221;Connor&#8221;. Have now changed the code to:<br />
			$querystr .= &#8220;AND UPPER(Authors) COLLATE SQL_Latin1_General_Cp1_CI_AS LIKE &#8216;%&#8221;.strtoupper(ereg_replace(&#8220;&#8216;&#8221;,&#8221;_&#8221;,stripslashes($Authors))).&#8221;%&#8217;  &#8220;;</p>
<p>&#8230;and this appears to work &#8211; the underscore allowing for the apostrophe, and the % working for&#8230;everything else.</p>
<p>Thanks for providing me a forum to think through this in public ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: techiechick</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-301119</link>
		<dc:creator><![CDATA[techiechick]]></dc:creator>
		<pubDate>Thu, 14 Jun 2012 10:18:30 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-301119</guid>
		<description><![CDATA[Hmm. Doubling up single quotes stops me getting the &quot;unclosed quotation&quot; error, however it&#039;s not pattern matching the data in the way I would hope. Searching on &quot;Connor&quot; brings back many results (which are displayed as &quot;O&#039;Connor&quot;) whereas searching on &quot;O&#039;Connor&quot; brings back none.

this is where my php code adds the relevant line to the query...
		$querystr .= &quot;AND UPPER(Authors) COLLATE SQL_Latin1_General_Cp1_CI_AS LIKE &#039;%&quot;.strtoupper(ereg_replace(&quot;&#039;&quot;,&quot;&#039;&#039;&quot;,$Authors)).&quot;%&#039;  &quot;;
.. which once built, is then run against the mssql DB.
Any &amp; all ideas welcome...!]]></description>
		<content:encoded><![CDATA[<p>Hmm. Doubling up single quotes stops me getting the &#8220;unclosed quotation&#8221; error, however it&#8217;s not pattern matching the data in the way I would hope. Searching on &#8220;Connor&#8221; brings back many results (which are displayed as &#8220;O&#8217;Connor&#8221;) whereas searching on &#8220;O&#8217;Connor&#8221; brings back none.</p>
<p>this is where my php code adds the relevant line to the query&#8230;<br />
		$querystr .= &#8220;AND UPPER(Authors) COLLATE SQL_Latin1_General_Cp1_CI_AS LIKE &#8216;%&#8221;.strtoupper(ereg_replace(&#8220;&#8216;&#8221;,&#8221;&#8221;&#8221;,$Authors)).&#8221;%&#8217;  &#8220;;<br />
.. which once built, is then run against the mssql DB.<br />
Any &amp; all ideas welcome&#8230;!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: swansoft</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-290292</link>
		<dc:creator><![CDATA[swansoft]]></dc:creator>
		<pubDate>Tue, 29 May 2012 07:43:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-290292</guid>
		<description><![CDATA[Dear Sir,

If I have a batch statement containing &quot;Database, Tables, Stored Procedures&quot; creation, then there&#039;s gonna be thousands of lines containing single quote. It would be difficult to find each one and replacing with two singles.

Isn&#039;t there a way like what @ operator doses in C# ?

Please help me to resolve this issue.


Thank You

Amol]]></description>
		<content:encoded><![CDATA[<p>Dear Sir,</p>
<p>If I have a batch statement containing &#8220;Database, Tables, Stored Procedures&#8221; creation, then there&#8217;s gonna be thousands of lines containing single quote. It would be difficult to find each one and replacing with two singles.</p>
<p>Isn&#8217;t there a way like what @ operator doses in C# ?</p>
<p>Please help me to resolve this issue.</p>
<p>Thank You</p>
<p>Amol</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: swansoft</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-290283</link>
		<dc:creator><![CDATA[swansoft]]></dc:creator>
		<pubDate>Tue, 29 May 2012 07:30:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-290283</guid>
		<description><![CDATA[Dear Sir,

I am trying to execute a batch statement which includes &#039;Database, Tables, Stored Procedures Creation etc.&#039;

It is a very big sql string. Here it would be difficult to find every single quote and replace it with two single quotes...

Isn&#039;t there any option at the start of the string only ( like &#039;@&#039; in C#) so that I can keep my whole string as it is. It will save a lot of time.

Please help me to resole this.


Thank You.

Amol]]></description>
		<content:encoded><![CDATA[<p>Dear Sir,</p>
<p>I am trying to execute a batch statement which includes &#8216;Database, Tables, Stored Procedures Creation etc.&#8217;</p>
<p>It is a very big sql string. Here it would be difficult to find every single quote and replace it with two single quotes&#8230;</p>
<p>Isn&#8217;t there any option at the start of the string only ( like &#8216;@&#8217; in C#) so that I can keep my whole string as it is. It will save a lot of time.</p>
<p>Please help me to resole this.</p>
<p>Thank You.</p>
<p>Amol</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Devang</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-277856</link>
		<dc:creator><![CDATA[Devang]]></dc:creator>
		<pubDate>Fri, 20 Apr 2012 12:51:53 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-277856</guid>
		<description><![CDATA[I have a table fields which contains characters like  &#039;s  and because of SQL limitation I am getting errors in application. Such characters sequence stored because of laps during conversion time. Can any one help me to replace escape &#039; characters with other one...

Thanks...

Devang]]></description>
		<content:encoded><![CDATA[<p>I have a table fields which contains characters like  &#8216;s  and because of SQL limitation I am getting errors in application. Such characters sequence stored because of laps during conversion time. Can any one help me to replace escape &#8216; characters with other one&#8230;</p>
<p>Thanks&#8230;</p>
<p>Devang</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nikhil</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-272457</link>
		<dc:creator><![CDATA[Nikhil]]></dc:creator>
		<pubDate>Thu, 05 Apr 2012 12:25:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-272457</guid>
		<description><![CDATA[Kumar, What is the final objective
Are you trying to remove the first letter of string and present the rest
OR
Are you trying to get the last four letters from the string.
Please elaborate.
bhattji]]></description>
		<content:encoded><![CDATA[<p>Kumar, What is the final objective<br />
Are you trying to remove the first letter of string and present the rest<br />
OR<br />
Are you trying to get the last four letters from the string.<br />
Please elaborate.<br />
bhattji</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kumar</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-272443</link>
		<dc:creator><![CDATA[kumar]]></dc:creator>
		<pubDate>Thu, 05 Apr 2012 11:57:45 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-272443</guid>
		<description><![CDATA[Hi pinal dev 

i have a question like this.....

i have a string say s=&#039;kumar&#039; and i want to insert &#039;UMAR&#039; only into my data base pls give me a query.]]></description>
		<content:encoded><![CDATA[<p>Hi pinal dev </p>
<p>i have a question like this&#8230;..</p>
<p>i have a string say s=&#8217;kumar&#8217; and i want to insert &#8216;UMAR&#8217; only into my data base pls give me a query.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bhupendra Negi</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-268203</link>
		<dc:creator><![CDATA[Bhupendra Negi]]></dc:creator>
		<pubDate>Tue, 27 Mar 2012 06:27:54 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-268203</guid>
		<description><![CDATA[Hello Friends, I am using a SP to export yearly Responses to an excel file by providing year to an SP, the @ForYear variable captures the parameter. Excel file code and other stuff is working fine when i am hard coding the @ForYear as 2012 in the dynamic SQL. I am using dynamic SQL and inserting the SQL parameter (@ForYear) inside it and then executing the Dynamic SQL based of file exist logic

--WHEN I EXECUTE THE SP

exec Proc_ExportEntireYearlyResponsesToExcel 2012

--THE ERROR IS

Unclosed quotation mark after the character string &#039;)SELECT * FROM @PVTable&#039;.

AGAIN WHEN I HARDCODE THE @ForYear as 2012 or 2011 data is coming in the excel file and there is no error

PLEASE HELP ITS AN EOD TASK

--Query starts here

ALTER PROCEDURE [dbo].[Proc_ExportEntireYearlyResponsesToExcel] 
	-- Add the parameters for the stored procedure here
	 @ForYear Varchar(250)
AS
BEGIN

EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE
EXEC sp_configure &#039;Ad Hoc Distributed Queries&#039;, 1; RECONFIGURE

Declare @Var1 as nvarchar(MAX)
set @Var1=&#039;&#039;&#039;&#039;+ &#039;Microsoft.ACE.OLEDB.12.0&#039; +&#039;&#039;&#039;&#039; +&#039;,&#039;
Declare @Var2 as nvarchar(MAX)
set @Var2=&#039;&#039;&#039;&#039;+&#039;Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;&#039;+&#039;&#039;&#039;&#039; +&#039;,&#039;
Declare @Var3 as nvarchar(MAX)
set @Var3=&#039;&#039;&#039;&#039;+&#039;SELECT * FROM [Sheet2$]&#039;+&#039;&#039;&#039;&#039;
declare @Var4 as nvarchar(max)
set @Var4=@Var1+@Var2+@Var3
PRINT &#039;OLEDB for Office 2007 --&gt; &#039;+@Var4 -- Correctly printed [OLEDB for Office 2007 --&gt; &#039;Microsoft.ACE.OLEDB.12.0&#039;,&#039;Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;&#039;,&#039;SELECT * FROM [Sheet2$]&#039;]

Declare @ResponsesQuery varchar(MAX)
set @ResponsesQuery=
(&#039;
declare @QCol table
(
      ColumnId                      int identity(1,1),
      QuestionIdentifierID    int
)
            
insert into 
      @QCol (QuestionIdentifierID) 
select 
      QuestionIdentifierID 
from 
      dbo.QuestionIdentifier as qi with(nolock) left join 
      dbo.Question as q with(nolock) on qi.QuestionID = q.QuestionID
where 
      q.SurveyID = 1

declare @PVTable table
(
	RespondentID int,
	IndustryName varchar(250), 
	BusinessSectorName varchar(250),
	CompanyName varchar(250),
	OpenDateTime varchar(250),  
	[1] varchar(250),
	[2] varchar(250),
	[3] varchar(250),
	[4] varchar(250),
	[5] varchar(250),
	[6] varchar(250)
)

insert into @PVTable
select
	RespondentID,
	IndustryName, 
	BusinessSectorName,
	CompanyName, 
	CONVERT(CHAR(4),OpenDateTime,120) as  OpenDateTime,
	[1] as Q18_1,
	[2] as Q18_2,
	[3] as Q18_3,
	[4] as Q12_1,
	[5] as Q12_2,
	[6] as Q36_1

from 
(
      select 
            qt1.RespondentID,
            qt1.IndustryName, 
            qt1.BusinessSectorName,
            qt1.CompanyName,
			qt1.OpenDateTime

 from
            @QCol as qc join
            (
                  select distinct
                        rt.RespondentID,
                        i.[Name] as IndustryName, 
                        bs.[Name] as BusinessSectorName,
                        cmp.[Name] as CompanyName, 
						sc.OpenDateTime,
                  from
                        dbo.Table1 rt left join 
                        dbo.Table2 r on rt.RespondentID = r.RespondentID left join
						.
						.
						.
						.
                        
                  where 
                        c.ChoiceListID is null and
						CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#039;+@ForYear+&#039;
            ) qt1 on qc.QuestionIdentifierID = qt1.QuestionIdentifierId


union
            select
                  qt2.RespondentID,
                  qt2.IndustryName, 
                  qt2.BusinessSectorName,
                  qt2.CompanyName,
				  qt2.OpenDateTime, 
                  
            from
                  @QCol as qc join
                  (
                  select distinct
                        t1.RespondentID,
                        t1.IndustryName, 
                        t1.BusinessSectorName,
                        t1.CompanyName,
						t1.OpenDateTime, 
                  from
                        dbo.Choice c join
                        (
                              select
                                    rt.RespondentID,
                                    i.[Name] as IndustryName, 
                                    bs.[Name] as BusinessSectorName,
                                    cmp.[Name] as CompanyName,
									sc.OpenDateTime, 
                              from
                                    dbo.Table1 rt left join 
                                    dbo.Table2 r on rt.RespondentID = r.RespondentID left join
                                    .
									.
									.
									.
			                        
                  where 

CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#039;+@ForYear+&#039;
                              
                        ) t1 on c.ChoiceListID = t1.ChoiceListID and t1.AnswerValue = cast(c.[Value] as varchar)
                  ) qt2 on qc.QuestionIdentifierID = qt2.QuestionIdentifierID
      ) t2

pivot
      (
            max([AnswerValue])
            for QuestionIdentifierID in
            ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50],[51],[52],[53],[54],[55],[56],[57],[58],[59],[60],[61],[62],[63],[64],[65],[66],[67],[68],[69],[70],[71],[72],[73],[74],[75],[76],[77],[78],[79],[80],[81],[82],[83],[84],[85],[86],[87],[88],[89],[90],[91],[92],[93],[94],[95],[96],[97],[98],[99],[100],[101],[102],[103],[104],[105])
      ) as pvt
            order by RespondentID;

INSERT INTO OPENROWSET (&#039;+@Var4+&#039;)SELECT * FROM @PVTable


&#039;)
--FILE MANUPLATION CODE HERE and it works fine
EXEC Master.dbo.xp_fileexist @FileToBeDeletedOnExistance, @File_Exists OUT
IF @File_Exists = 1
	begin
		execute (@ResponsesQuery) -- error is here 
	END
ELSE 
	begin
		-- any other query
	END

--Query ends here

--WHEN I EXECUTE THE SP

exec Proc_ExportEntireYearlyResponsesToExcel 2012

--THE ERROR IS

Unclosed quotation mark after the character string &#039;)SELECT * FROM @PVTable&#039;.]]></description>
		<content:encoded><![CDATA[<p>Hello Friends, I am using a SP to export yearly Responses to an excel file by providing year to an SP, the @ForYear variable captures the parameter. Excel file code and other stuff is working fine when i am hard coding the @ForYear as 2012 in the dynamic SQL. I am using dynamic SQL and inserting the SQL parameter (@ForYear) inside it and then executing the Dynamic SQL based of file exist logic</p>
<p>&#8211;WHEN I EXECUTE THE SP</p>
<p>exec Proc_ExportEntireYearlyResponsesToExcel 2012</p>
<p>&#8211;THE ERROR IS</p>
<p>Unclosed quotation mark after the character string &#8216;)SELECT * FROM @PVTable&#8217;.</p>
<p>AGAIN WHEN I HARDCODE THE @ForYear as 2012 or 2011 data is coming in the excel file and there is no error</p>
<p>PLEASE HELP ITS AN EOD TASK</p>
<p>&#8211;Query starts here</p>
<p>ALTER PROCEDURE [dbo].[Proc_ExportEntireYearlyResponsesToExcel]<br />
	&#8211; Add the parameters for the stored procedure here<br />
	 @ForYear Varchar(250)<br />
AS<br />
BEGIN</p>
<p>EXEC sp_configure &#8216;show advanced options&#8217;, 1; RECONFIGURE<br />
EXEC sp_configure &#8216;Ad Hoc Distributed Queries&#8217;, 1; RECONFIGURE</p>
<p>Declare @Var1 as nvarchar(MAX)<br />
set @Var1=&#8221;&#8221;+ &#8216;Microsoft.ACE.OLEDB.12.0&#8242; +&#8221;&#8221; +&#8217;,&#8217;<br />
Declare @Var2 as nvarchar(MAX)<br />
set @Var2=&#8221;&#8221;+&#8217;Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;&#8217;+&#8221;&#8221; +&#8217;,&#8217;<br />
Declare @Var3 as nvarchar(MAX)<br />
set @Var3=&#8221;&#8221;+&#8217;SELECT * FROM [Sheet2$]&#8216;+&#8221;&#8221;<br />
declare @Var4 as nvarchar(max)<br />
set @Var4=@Var1+@Var2+@Var3<br />
PRINT &#8216;OLEDB for Office 2007 &#8211;&gt; &#8216;+@Var4 &#8212; Correctly printed [OLEDB for Office 2007 --&gt; 'Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;','SELECT * FROM [Sheet2$]&#8216;]</p>
<p>Declare @ResponsesQuery varchar(MAX)<br />
set @ResponsesQuery=<br />
(&#8216;<br />
declare @QCol table<br />
(<br />
      ColumnId                      int identity(1,1),<br />
      QuestionIdentifierID    int<br />
)</p>
<p>insert into<br />
      @QCol (QuestionIdentifierID)<br />
select<br />
      QuestionIdentifierID<br />
from<br />
      dbo.QuestionIdentifier as qi with(nolock) left join<br />
      dbo.Question as q with(nolock) on qi.QuestionID = q.QuestionID<br />
where<br />
      q.SurveyID = 1</p>
<p>declare @PVTable table<br />
(<br />
	RespondentID int,<br />
	IndustryName varchar(250),<br />
	BusinessSectorName varchar(250),<br />
	CompanyName varchar(250),<br />
	OpenDateTime varchar(250),<br />
	[1] varchar(250),<br />
	[2] varchar(250),<br />
	[3] varchar(250),<br />
	[4] varchar(250),<br />
	[5] varchar(250),<br />
	[6] varchar(250)<br />
)</p>
<p>insert into @PVTable<br />
select<br />
	RespondentID,<br />
	IndustryName,<br />
	BusinessSectorName,<br />
	CompanyName,<br />
	CONVERT(CHAR(4),OpenDateTime,120) as  OpenDateTime,<br />
	[1] as Q18_1,<br />
	[2] as Q18_2,<br />
	[3] as Q18_3,<br />
	[4] as Q12_1,<br />
	[5] as Q12_2,<br />
	[6] as Q36_1</p>
<p>from<br />
(<br />
      select<br />
            qt1.RespondentID,<br />
            qt1.IndustryName,<br />
            qt1.BusinessSectorName,<br />
            qt1.CompanyName,<br />
			qt1.OpenDateTime</p>
<p> from<br />
            @QCol as qc join<br />
            (<br />
                  select distinct<br />
                        rt.RespondentID,<br />
                        i.[Name] as IndustryName,<br />
                        bs.[Name] as BusinessSectorName,<br />
                        cmp.[Name] as CompanyName,<br />
						sc.OpenDateTime,<br />
                  from<br />
                        dbo.Table1 rt left join<br />
                        dbo.Table2 r on rt.RespondentID = r.RespondentID left join<br />
						.<br />
						.<br />
						.<br />
						.</p>
<p>                  where<br />
                        c.ChoiceListID is null and<br />
						CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#8217;+@ForYear+&#8217;<br />
            ) qt1 on qc.QuestionIdentifierID = qt1.QuestionIdentifierId</p>
<p>union<br />
            select<br />
                  qt2.RespondentID,<br />
                  qt2.IndustryName,<br />
                  qt2.BusinessSectorName,<br />
                  qt2.CompanyName,<br />
				  qt2.OpenDateTime, </p>
<p>            from<br />
                  @QCol as qc join<br />
                  (<br />
                  select distinct<br />
                        t1.RespondentID,<br />
                        t1.IndustryName,<br />
                        t1.BusinessSectorName,<br />
                        t1.CompanyName,<br />
						t1.OpenDateTime,<br />
                  from<br />
                        dbo.Choice c join<br />
                        (<br />
                              select<br />
                                    rt.RespondentID,<br />
                                    i.[Name] as IndustryName,<br />
                                    bs.[Name] as BusinessSectorName,<br />
                                    cmp.[Name] as CompanyName,<br />
									sc.OpenDateTime,<br />
                              from<br />
                                    dbo.Table1 rt left join<br />
                                    dbo.Table2 r on rt.RespondentID = r.RespondentID left join<br />
                                    .<br />
									.<br />
									.<br />
									.</p>
<p>                  where </p>
<p>CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#8217;+@ForYear+&#8217;</p>
<p>                        ) t1 on c.ChoiceListID = t1.ChoiceListID and t1.AnswerValue = cast(c.[Value] as varchar)<br />
                  ) qt2 on qc.QuestionIdentifierID = qt2.QuestionIdentifierID<br />
      ) t2</p>
<p>pivot<br />
      (<br />
            max([AnswerValue])<br />
            for QuestionIdentifierID in<br />
            ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50],[51],[52],[53],[54],[55],[56],[57],[58],[59],[60],[61],[62],[63],[64],[65],[66],[67],[68],[69],[70],[71],[72],[73],[74],[75],[76],[77],[78],[79],[80],[81],[82],[83],[84],[85],[86],[87],[88],[89],[90],[91],[92],[93],[94],[95],[96],[97],[98],[99],[100],[101],[102],[103],[104],[105])<br />
      ) as pvt<br />
            order by RespondentID;</p>
<p>INSERT INTO OPENROWSET (&#8216;+@Var4+&#8217;)SELECT * FROM @PVTable</p>
<p>&#8216;)<br />
&#8211;FILE MANUPLATION CODE HERE and it works fine<br />
EXEC Master.dbo.xp_fileexist @FileToBeDeletedOnExistance, @File_Exists OUT<br />
IF @File_Exists = 1<br />
	begin<br />
		execute (@ResponsesQuery) &#8212; error is here<br />
	END<br />
ELSE<br />
	begin<br />
		&#8211; any other query<br />
	END</p>
<p>&#8211;Query ends here</p>
<p>&#8211;WHEN I EXECUTE THE SP</p>
<p>exec Proc_ExportEntireYearlyResponsesToExcel 2012</p>
<p>&#8211;THE ERROR IS</p>
<p>Unclosed quotation mark after the character string &#8216;)SELECT * FROM @PVTable&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bhupendra Negi</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-268200</link>
		<dc:creator><![CDATA[Bhupendra Negi]]></dc:creator>
		<pubDate>Tue, 27 Mar 2012 06:21:54 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-268200</guid>
		<description><![CDATA[I am creating a SP the exports DB responses to Excel file, excel file code is working fine, I am using Dynamic SQL, when i am appending SP parameter variable inside the dynamic query i am facing issues of &#039; (quotes) i think .The SP parameter is stored in @ForYear variable. when i am hard coding the @ForYear with say 2012 the query is executing without error. please help. 

--WHEN I EXECUTE THE SP

exec Proc_ExportEntireYearlyResponsesToExcel 2012

--THE ERROR IS

Unclosed quotation mark after the character string &#039;)SELECT * FROM @PVTable&#039;.


ALTER PROCEDURE [dbo].[Proc_ExportEntireYearlyResponsesToExcel] 
	-- Add the parameters for the stored procedure here
	 @ForYear Varchar(250)
AS
BEGIN

EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE
EXEC sp_configure &#039;Ad Hoc Distributed Queries&#039;, 1; RECONFIGURE

Declare @Var1 as nvarchar(MAX)
set @Var1=&#039;&#039;&#039;&#039;+ &#039;Microsoft.ACE.OLEDB.12.0&#039; +&#039;&#039;&#039;&#039; +&#039;,&#039;
Declare @Var2 as nvarchar(MAX)
set @Var2=&#039;&#039;&#039;&#039;+&#039;Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;&#039;+&#039;&#039;&#039;&#039; +&#039;,&#039;
Declare @Var3 as nvarchar(MAX)
set @Var3=&#039;&#039;&#039;&#039;+&#039;SELECT * FROM [Sheet2$]&#039;+&#039;&#039;&#039;&#039;
declare @Var4 as nvarchar(max)
set @Var4=@Var1+@Var2+@Var3
PRINT &#039;OLEDB for Office 2007 --&gt; &#039;+@Var4 -- Correctly printed [OLEDB for Office 2007 --&gt; &#039;Microsoft.ACE.OLEDB.12.0&#039;,&#039;Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;&#039;,&#039;SELECT * FROM [Sheet2$]&#039;]

Declare @ResponsesQuery varchar(MAX)
set @ResponsesQuery=
(&#039;
declare @QCol table
(
      ColumnId                      int identity(1,1),
      QuestionIdentifierID    int
)
            
insert into 
      @QCol (QuestionIdentifierID) 
select 
      QuestionIdentifierID 
from 
      dbo.QuestionIdentifier as qi with(nolock) left join 
      dbo.Question as q with(nolock) on qi.QuestionID = q.QuestionID
where 
      q.SurveyID = 1

declare @PVTable table
(
	RespondentID int,
	IndustryName varchar(250), 
	BusinessSectorName varchar(250),
	CompanyName varchar(250),
	OpenDateTime varchar(250),  
	[1] varchar(250),
	[2] varchar(250),
	[3] varchar(250),
	[4] varchar(250),
	[5] varchar(250),
	[6] varchar(250)
)

insert into @PVTable
select
	RespondentID,
	IndustryName, 
	BusinessSectorName,
	CompanyName, 
	CONVERT(CHAR(4),OpenDateTime,120) as  OpenDateTime,
	[1] as Q18_1,
	[2] as Q18_2,
	[3] as Q18_3,
	[4] as Q12_1,
	[5] as Q12_2,
	[6] as Q36_1

from 
(
      select 
            qt1.RespondentID,
            qt1.IndustryName, 
            qt1.BusinessSectorName,
            qt1.CompanyName,
			qt1.OpenDateTime

 from
            @QCol as qc join
            (
                  select distinct
                        rt.RespondentID,
                        i.[Name] as IndustryName, 
                        bs.[Name] as BusinessSectorName,
                        cmp.[Name] as CompanyName, 
						sc.OpenDateTime,
                  from
                        dbo.Table1 rt left join 
                        dbo.Table2 r on rt.RespondentID = r.RespondentID left join
						.
						.
						.
						.
                        
                  where 
                        c.ChoiceListID is null and
						CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#039;+@ForYear+&#039;
            ) qt1 on qc.QuestionIdentifierID = qt1.QuestionIdentifierId


union
            select
                  qt2.RespondentID,
                  qt2.IndustryName, 
                  qt2.BusinessSectorName,
                  qt2.CompanyName,
				  qt2.OpenDateTime, 
                  
            from
                  @QCol as qc join
                  (
                  select distinct
                        t1.RespondentID,
                        t1.IndustryName, 
                        t1.BusinessSectorName,
                        t1.CompanyName,
						t1.OpenDateTime, 
                  from
                        dbo.Choice c join
                        (
                              select
                                    rt.RespondentID,
                                    i.[Name] as IndustryName, 
                                    bs.[Name] as BusinessSectorName,
                                    cmp.[Name] as CompanyName,
									sc.OpenDateTime, 
                              from
                                    dbo.Table1 rt left join 
                                    dbo.Table2 r on rt.RespondentID = r.RespondentID left join
                                    .
									.
									.
									.
			                        
                  where 

CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#039;+@ForYear+&#039;
                              
                        ) t1 on c.ChoiceListID = t1.ChoiceListID and t1.AnswerValue = cast(c.[Value] as varchar)
                  ) qt2 on qc.QuestionIdentifierID = qt2.QuestionIdentifierID
      ) t2

pivot
      (
            max([AnswerValue])
            for QuestionIdentifierID in
            ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50],[51],[52],[53],[54],[55],[56],[57],[58],[59],[60],[61],[62],[63],[64],[65],[66],[67],[68],[69],[70],[71],[72],[73],[74],[75],[76],[77],[78],[79],[80],[81],[82],[83],[84],[85],[86],[87],[88],[89],[90],[91],[92],[93],[94],[95],[96],[97],[98],[99],[100],[101],[102],[103],[104],[105])
      ) as pvt
            order by RespondentID;

INSERT INTO OPENROWSET (&#039;+@Var4+&#039;) SELECT * FROM @PVTable
&#039;)

--FILE MANUPLATION CODE HERE and it works fine
EXEC Master.dbo.xp_fileexist @FileToBeDeletedOnExistance, @File_Exists OUT
IF @File_Exists = 1
	begin
		execute (@ResponsesQuery) -- error is here 
	END
ELSE 
	begin
		-- any other query
	END


--WHEN I EXECUTE THE SP

exec Proc_ExportEntireYearlyResponsesToExcel 2012

--THE ERROR IS

Unclosed quotation mark after the character string &#039;)SELECT * FROM @PVTable&#039;.]]></description>
		<content:encoded><![CDATA[<p>I am creating a SP the exports DB responses to Excel file, excel file code is working fine, I am using Dynamic SQL, when i am appending SP parameter variable inside the dynamic query i am facing issues of &#8216; (quotes) i think .The SP parameter is stored in @ForYear variable. when i am hard coding the @ForYear with say 2012 the query is executing without error. please help. </p>
<p>&#8211;WHEN I EXECUTE THE SP</p>
<p>exec Proc_ExportEntireYearlyResponsesToExcel 2012</p>
<p>&#8211;THE ERROR IS</p>
<p>Unclosed quotation mark after the character string &#8216;)SELECT * FROM @PVTable&#8217;.</p>
<p>ALTER PROCEDURE [dbo].[Proc_ExportEntireYearlyResponsesToExcel]<br />
	&#8211; Add the parameters for the stored procedure here<br />
	 @ForYear Varchar(250)<br />
AS<br />
BEGIN</p>
<p>EXEC sp_configure &#8216;show advanced options&#8217;, 1; RECONFIGURE<br />
EXEC sp_configure &#8216;Ad Hoc Distributed Queries&#8217;, 1; RECONFIGURE</p>
<p>Declare @Var1 as nvarchar(MAX)<br />
set @Var1=&#8221;&#8221;+ &#8216;Microsoft.ACE.OLEDB.12.0&#8242; +&#8221;&#8221; +&#8217;,&#8217;<br />
Declare @Var2 as nvarchar(MAX)<br />
set @Var2=&#8221;&#8221;+&#8217;Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;&#8217;+&#8221;&#8221; +&#8217;,&#8217;<br />
Declare @Var3 as nvarchar(MAX)<br />
set @Var3=&#8221;&#8221;+&#8217;SELECT * FROM [Sheet2$]&#8216;+&#8221;&#8221;<br />
declare @Var4 as nvarchar(max)<br />
set @Var4=@Var1+@Var2+@Var3<br />
PRINT &#8216;OLEDB for Office 2007 &#8211;&gt; &#8216;+@Var4 &#8212; Correctly printed [OLEDB for Office 2007 --&gt; 'Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:\destination\EntireYearlyMESRespondentResponses.xlsx;','SELECT * FROM [Sheet2$]&#8216;]</p>
<p>Declare @ResponsesQuery varchar(MAX)<br />
set @ResponsesQuery=<br />
(&#8216;<br />
declare @QCol table<br />
(<br />
      ColumnId                      int identity(1,1),<br />
      QuestionIdentifierID    int<br />
)</p>
<p>insert into<br />
      @QCol (QuestionIdentifierID)<br />
select<br />
      QuestionIdentifierID<br />
from<br />
      dbo.QuestionIdentifier as qi with(nolock) left join<br />
      dbo.Question as q with(nolock) on qi.QuestionID = q.QuestionID<br />
where<br />
      q.SurveyID = 1</p>
<p>declare @PVTable table<br />
(<br />
	RespondentID int,<br />
	IndustryName varchar(250),<br />
	BusinessSectorName varchar(250),<br />
	CompanyName varchar(250),<br />
	OpenDateTime varchar(250),<br />
	[1] varchar(250),<br />
	[2] varchar(250),<br />
	[3] varchar(250),<br />
	[4] varchar(250),<br />
	[5] varchar(250),<br />
	[6] varchar(250)<br />
)</p>
<p>insert into @PVTable<br />
select<br />
	RespondentID,<br />
	IndustryName,<br />
	BusinessSectorName,<br />
	CompanyName,<br />
	CONVERT(CHAR(4),OpenDateTime,120) as  OpenDateTime,<br />
	[1] as Q18_1,<br />
	[2] as Q18_2,<br />
	[3] as Q18_3,<br />
	[4] as Q12_1,<br />
	[5] as Q12_2,<br />
	[6] as Q36_1</p>
<p>from<br />
(<br />
      select<br />
            qt1.RespondentID,<br />
            qt1.IndustryName,<br />
            qt1.BusinessSectorName,<br />
            qt1.CompanyName,<br />
			qt1.OpenDateTime</p>
<p> from<br />
            @QCol as qc join<br />
            (<br />
                  select distinct<br />
                        rt.RespondentID,<br />
                        i.[Name] as IndustryName,<br />
                        bs.[Name] as BusinessSectorName,<br />
                        cmp.[Name] as CompanyName,<br />
						sc.OpenDateTime,<br />
                  from<br />
                        dbo.Table1 rt left join<br />
                        dbo.Table2 r on rt.RespondentID = r.RespondentID left join<br />
						.<br />
						.<br />
						.<br />
						.</p>
<p>                  where<br />
                        c.ChoiceListID is null and<br />
						CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#8217;+@ForYear+&#8217;<br />
            ) qt1 on qc.QuestionIdentifierID = qt1.QuestionIdentifierId</p>
<p>union<br />
            select<br />
                  qt2.RespondentID,<br />
                  qt2.IndustryName,<br />
                  qt2.BusinessSectorName,<br />
                  qt2.CompanyName,<br />
				  qt2.OpenDateTime, </p>
<p>            from<br />
                  @QCol as qc join<br />
                  (<br />
                  select distinct<br />
                        t1.RespondentID,<br />
                        t1.IndustryName,<br />
                        t1.BusinessSectorName,<br />
                        t1.CompanyName,<br />
						t1.OpenDateTime,<br />
                  from<br />
                        dbo.Choice c join<br />
                        (<br />
                              select<br />
                                    rt.RespondentID,<br />
                                    i.[Name] as IndustryName,<br />
                                    bs.[Name] as BusinessSectorName,<br />
                                    cmp.[Name] as CompanyName,<br />
									sc.OpenDateTime,<br />
                              from<br />
                                    dbo.Table1 rt left join<br />
                                    dbo.Table2 r on rt.RespondentID = r.RespondentID left join<br />
                                    .<br />
									.<br />
									.<br />
									.</p>
<p>                  where </p>
<p>CONVERT(CHAR(4), sc.OpenDateTime, 120)=&#8217;+@ForYear+&#8217;</p>
<p>                        ) t1 on c.ChoiceListID = t1.ChoiceListID and t1.AnswerValue = cast(c.[Value] as varchar)<br />
                  ) qt2 on qc.QuestionIdentifierID = qt2.QuestionIdentifierID<br />
      ) t2</p>
<p>pivot<br />
      (<br />
            max([AnswerValue])<br />
            for QuestionIdentifierID in<br />
            ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50],[51],[52],[53],[54],[55],[56],[57],[58],[59],[60],[61],[62],[63],[64],[65],[66],[67],[68],[69],[70],[71],[72],[73],[74],[75],[76],[77],[78],[79],[80],[81],[82],[83],[84],[85],[86],[87],[88],[89],[90],[91],[92],[93],[94],[95],[96],[97],[98],[99],[100],[101],[102],[103],[104],[105])<br />
      ) as pvt<br />
            order by RespondentID;</p>
<p>INSERT INTO OPENROWSET (&#8216;+@Var4+&#8217;) SELECT * FROM @PVTable<br />
&#8216;)</p>
<p>&#8211;FILE MANUPLATION CODE HERE and it works fine<br />
EXEC Master.dbo.xp_fileexist @FileToBeDeletedOnExistance, @File_Exists OUT<br />
IF @File_Exists = 1<br />
	begin<br />
		execute (@ResponsesQuery) &#8212; error is here<br />
	END<br />
ELSE<br />
	begin<br />
		&#8211; any other query<br />
	END</p>
<p>&#8211;WHEN I EXECUTE THE SP</p>
<p>exec Proc_ExportEntireYearlyResponsesToExcel 2012</p>
<p>&#8211;THE ERROR IS</p>
<p>Unclosed quotation mark after the character string &#8216;)SELECT * FROM @PVTable&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gilmer</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-262633</link>
		<dc:creator><![CDATA[Gilmer]]></dc:creator>
		<pubDate>Tue, 13 Mar 2012 18:44:01 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-262633</guid>
		<description><![CDATA[Excellent Blog]]></description>
		<content:encoded><![CDATA[<p>Excellent Blog</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Siridion M. Cabudlan</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-242921</link>
		<dc:creator><![CDATA[Siridion M. Cabudlan]]></dc:creator>
		<pubDate>Tue, 24 Jan 2012 08:01:07 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-242921</guid>
		<description><![CDATA[Thanks.]]></description>
		<content:encoded><![CDATA[<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tanveer</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-232448</link>
		<dc:creator><![CDATA[Tanveer]]></dc:creator>
		<pubDate>Fri, 06 Jan 2012 14:18:50 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-232448</guid>
		<description><![CDATA[I have getting same problem in SQL string kindly help me out about this error my sring is 
&quot;INSERT INTO LoginMaster(UserName, Password, DistrictID, AgencyID, Address, BlockID, PanchayatID, UserType, ContactNo, EmailAddress, SQuestion, SAnswer, Status) VALUES (&#039;&quot; + UserName.Text + &quot;&#039;, &#039;&quot; &amp; Password.Text &amp; &quot;&#039;,&#039;&quot; &amp; district.SelectedValue &amp; &quot;&#039;,&#039;&quot; &amp; agency.SelectedValue &amp; &quot;&#039;,&#039;&quot; &amp; AgencyAddress.Text &amp; &quot;&#039;,&#039;0&#039;,&#039;0&#039;,&#039;&quot; &amp; UserType.SelectedValue &amp; &quot;&#039;,&#039;&quot; &amp; contactno.Text &amp; &quot;&#039;,&#039;&quot; &amp; emailaddress.Text &amp; &quot;&#039; ,&#039;&quot; &amp; SQuestion.Text &amp; &quot;&#039;,&#039;&quot; &amp; SAnswer.Text &amp; &quot;&#039;,&#039;1&#039;)&quot;

plz help me]]></description>
		<content:encoded><![CDATA[<p>I have getting same problem in SQL string kindly help me out about this error my sring is<br />
&#8220;INSERT INTO LoginMaster(UserName, Password, DistrictID, AgencyID, Address, BlockID, PanchayatID, UserType, ContactNo, EmailAddress, SQuestion, SAnswer, Status) VALUES (&#8216;&#8221; + UserName.Text + &#8220;&#8216;, &#8216;&#8221; &amp; Password.Text &amp; &#8220;&#8216;,&#8217;&#8221; &amp; district.SelectedValue &amp; &#8220;&#8216;,&#8217;&#8221; &amp; agency.SelectedValue &amp; &#8220;&#8216;,&#8217;&#8221; &amp; AgencyAddress.Text &amp; &#8220;&#8216;,&#8217;0&#8242;,&#8217;0&#8242;,&#8217;&#8221; &amp; UserType.SelectedValue &amp; &#8220;&#8216;,&#8217;&#8221; &amp; contactno.Text &amp; &#8220;&#8216;,&#8217;&#8221; &amp; emailaddress.Text &amp; &#8220;&#8216; ,&#8217;&#8221; &amp; SQuestion.Text &amp; &#8220;&#8216;,&#8217;&#8221; &amp; SAnswer.Text &amp; &#8220;&#8216;,&#8217;1&#8242;)&#8221;</p>
<p>plz help me</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kumbair</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-215510</link>
		<dc:creator><![CDATA[kumbair]]></dc:creator>
		<pubDate>Sun, 11 Dec 2011 18:21:30 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-215510</guid>
		<description><![CDATA[Hi Pinal,

I am developing a .net application and as part of this, i have a stored procedure that gets me the data with required format. This stored procedure has dynamic sql in it where the formatting of the data takes place.

Below is the query that i am using to format any data based on its column datatype.  This query just does the following, I have used master DB for this example, 

1. @query variable is formed with a sql query where this query has a case statement in which column name is passed to scalar function(udfSelDataTypeOfColumn) and datatype is got from it.
2. If this datatype is of CHAR or NCHAR or NVARCHAR or VARCHAR then i want the output of the column value enclosed in a single quote.
Suppose, the output value is a varchar with value as Pinal then this query has to return &#039;Pinal&#039; else for other datatypes same value can be returned like (int)1234, (bit)1
I tried formatting with single quotes and concatinating but no luck.

Please let me know a solution to this problem and explain me how single quotes intepretation is done by sql runtime.

USE master

DECLARE @query NVARCHAR(MAX),
		@charDataType NVARCHAR(20)
		
SELECT @charDataType=&#039;&#039;&#039;%char%&#039;&#039;&#039;		
select @query=&#039;SELECT TOP 10
				CASE WHEN dbo.udfSelDataTypeOfColumn(&#039;&#039;name&#039;&#039;) like &#039;+@charDataType+ &#039;then &#039;&#039;&#039;&#039; +name+ &#039;&#039;&#039;&#039; else &#039;&#039;name&#039;&#039; end
				FROM spt_values WITH(NOLOCK)&#039;
EXEC sp_executesql @query]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I am developing a .net application and as part of this, i have a stored procedure that gets me the data with required format. This stored procedure has dynamic sql in it where the formatting of the data takes place.</p>
<p>Below is the query that i am using to format any data based on its column datatype.  This query just does the following, I have used master DB for this example, </p>
<p>1. @query variable is formed with a sql query where this query has a case statement in which column name is passed to scalar function(udfSelDataTypeOfColumn) and datatype is got from it.<br />
2. If this datatype is of CHAR or NCHAR or NVARCHAR or VARCHAR then i want the output of the column value enclosed in a single quote.<br />
Suppose, the output value is a varchar with value as Pinal then this query has to return &#8216;Pinal&#8217; else for other datatypes same value can be returned like (int)1234, (bit)1<br />
I tried formatting with single quotes and concatinating but no luck.</p>
<p>Please let me know a solution to this problem and explain me how single quotes intepretation is done by sql runtime.</p>
<p>USE master</p>
<p>DECLARE @query NVARCHAR(MAX),<br />
		@charDataType NVARCHAR(20)</p>
<p>SELECT @charDataType=&#8221;&#8217;%char%&#8221;&#8217;<br />
select @query=&#8217;SELECT TOP 10<br />
				CASE WHEN dbo.udfSelDataTypeOfColumn(&#8221;name&#8221;) like &#8216;+@charDataType+ &#8216;then &#8221;&#8221; +name+ &#8221;&#8221; else &#8221;name&#8221; end<br />
				FROM spt_values WITH(NOLOCK)&#8217;<br />
EXEC sp_executesql @query</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ABAnth</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-212373</link>
		<dc:creator><![CDATA[ABAnth]]></dc:creator>
		<pubDate>Tue, 06 Dec 2011 23:00:37 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-212373</guid>
		<description><![CDATA[thanks! Big fan of your posts]]></description>
		<content:encoded><![CDATA[<p>thanks! Big fan of your posts</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ola</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-208117</link>
		<dc:creator><![CDATA[Ola]]></dc:creator>
		<pubDate>Wed, 30 Nov 2011 17:59:44 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-208117</guid>
		<description><![CDATA[Thank you so much Nikhil Bhatt]]></description>
		<content:encoded><![CDATA[<p>Thank you so much Nikhil Bhatt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: karthik</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-167488</link>
		<dc:creator><![CDATA[karthik]]></dc:creator>
		<pubDate>Mon, 12 Sep 2011 11:22:27 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-167488</guid>
		<description><![CDATA[ALTER PROCEDURE [dbo].[DBSP_SF_GetAccountsLikeAccNo]
					@AccountNo VARCHAR(20),
					@BusinessName VARCHAR(40),
					@Type VARCHAR(2)
				AS
				BEGIN
DECLARE @str VARCHAR(100)
SELECT @str=REPLACE(@BusinessName,&#039;&#039;&#039;&#039;,&#039;&#039;)

					IF @Type = &#039;AN&#039;
					BEGIN
						SELECT AccountNo, BusinessName FROM CustomerDetails WITH (NOLOCK) WHERE AccountNo like @AccountNo + &#039;%&#039;
					END
					ELSE IF @Type = &#039;BN&#039;
					BEGIN
						SELECT AccountNo, BusinessName FROM CustomerDetails WITH (NOLOCK) WHERE BusinessName like @str + &#039;%&#039; 
							
					END
				END
if the value is Andersons The Grocery Store .in this I did not get any problem. But, &#039;Anderson&#039;s &quot;The Grocery Store&quot;‘ I am getting error like Unclosed quotation mark after the character string ‘). Can you please help out me by giving proper code (how to replace two single quotes in place of one single quote).
plz helpme any modify my SP]]></description>
		<content:encoded><![CDATA[<p>ALTER PROCEDURE [dbo].[DBSP_SF_GetAccountsLikeAccNo]<br />
					@AccountNo VARCHAR(20),<br />
					@BusinessName VARCHAR(40),<br />
					@Type VARCHAR(2)<br />
				AS<br />
				BEGIN<br />
DECLARE @str VARCHAR(100)<br />
SELECT @str=REPLACE(@BusinessName,&#8221;&#8221;,&#8221;)</p>
<p>					IF @Type = &#8216;AN&#8217;<br />
					BEGIN<br />
						SELECT AccountNo, BusinessName FROM CustomerDetails WITH (NOLOCK) WHERE AccountNo like @AccountNo + &#8216;%&#8217;<br />
					END<br />
					ELSE IF @Type = &#8216;BN&#8217;<br />
					BEGIN<br />
						SELECT AccountNo, BusinessName FROM CustomerDetails WITH (NOLOCK) WHERE BusinessName like @str + &#8216;%&#8217; </p>
<p>					END<br />
				END<br />
if the value is Andersons The Grocery Store .in this I did not get any problem. But, &#8216;Anderson&#8217;s &#8220;The Grocery Store&#8221;‘ I am getting error like Unclosed quotation mark after the character string ‘). Can you please help out me by giving proper code (how to replace two single quotes in place of one single quote).<br />
plz helpme any modify my SP</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph Antonaccio</title>
		<link>http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/#comment-162328</link>
		<dc:creator><![CDATA[Joseph Antonaccio]]></dc:creator>
		<pubDate>Fri, 26 Aug 2011 17:02:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=508#comment-162328</guid>
		<description><![CDATA[I am using a fully loaded sqlite3 database where embedded single and double quotes are included in many of the varchar fields. What select query could i write to simply query the database without things breaking?

for example
where col2 is know to contain embedded single and double quotes
Select col1, col2 from table_Name;]]></description>
		<content:encoded><![CDATA[<p>I am using a fully loaded sqlite3 database where embedded single and double quotes are included in many of the varchar fields. What select query could i write to simply query the database without things breaking?</p>
<p>for example<br />
where col2 is know to contain embedded single and double quotes<br />
Select col1, col2 from table_Name;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
