Solved

Dynamic SQL with DateTime conversion

Posted on 2008-10-09
9
707 Views
Last Modified: 2012-05-05
Having to convert a text string to datetime.  Once converted I'm comparing the new datetime variable to a real datetime in the database.  This works fine in regular SQL, but when I convert it to dynamic SQL I get the following error message:   "Syntax error converting datetime from character string."

I'm not especially strong with dynamic sql, so any assistance is appreciated.
DECLARE @TextMonthYear datetime
DECLARE @MyStatement varchar(8000)
 
SET @TextMonthYear = Dateadd(Month,1,CONVERT(datetime, '10' + '/1/' + '2006'))
 
SET @MyStatement = n'	SELECT 	*
			FROM 	TableName
			WHERE	TableName.DateField < '+@TextMonthYear+'' 
EXEC (@MyStatement)

Open in new window

0
Comment
Question by:jobprojn
  • 4
  • 4
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22682866
do you REALLY need dynamic sql?

if not:
DECLARE @TextMonthYear datetime
DECLARE @MyStatement varchar(8000)
 
SET @TextMonthYear = Dateadd(Month,1,CONVERT(datetime, '10' + '/1/' + '2006'))
 
SELECT       *
FROM       TableName
WHERE      TableName.DateField < @TextMonthYear  

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22682873
if yes:
DECLARE @TextMonthYear datetime
DECLARE @MyStatement varchar(8000)
 
SET @TextMonthYear = Dateadd(Month,1,CONVERT(datetime, '10' + '/1/' + '2006'))
 
SET @MyStatement = n'      SELECT       *
                  FROM       TableName
                  WHERE      TableName.DateField < @dt ' 
EXEC sp_executesql @MyStatement, N'@dt datetime', @TextMonthYear 

Open in new window

0
 

Author Comment

by:jobprojn
ID: 22682961
angell, unfortuately yes, I have to deal wtih dynamic sql.  I attempted your solution and get the following error message:  Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

Suggestions??
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22682991
sorry:
DECLARE @TextMonthYear datetime
DECLARE @MyStatement nvarchar(8000)
 
SET @TextMonthYear = Dateadd(Month,1,CONVERT(datetime, '10' + '/1/' + '2006'))
 
SET @MyStatement = n'      SELECT       *
                  FROM       TableName
                  WHERE      TableName.DateField < @dt ' 
EXEC sp_executesql @MyStatement, N'@dt datetime', @TextMonthYear 

Open in new window

0
 
LVL 35

Expert Comment

by:David Todd
ID: 22682993
Hi,

I've corrected Angell's script ...

Try now.

Cheers
  David
DECLARE @TextMonthYear datetime
DECLARE @MyStatement Nvarchar(8000)
 
SET @TextMonthYear = Dateadd(Month,1,CONVERT(datetime, '10' + '/1/' + '2006'))
 
SET @MyStatement = n'      SELECT       *
                  FROM       TableName
                  WHERE      TableName.DateField < @dt ' 
EXEC sp_executesql @MyStatement, N'@dt datetime', @TextMonthYear 

Open in new window

0
 

Author Comment

by:jobprojn
ID: 22683101
angellll and dtodd, it appears all three dynamic sql scripts are identical....... am I missing something?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 22683106
yes:
DECLARE @MyStatement Nvarchar(8000)

instead of:
DECLARE @MyStatement varchar(8000)

0
 

Author Closing Comment

by:jobprojn
ID: 31504851
You are the man!!  Thank you.
0
 

Author Comment

by:jobprojn
ID: 22683139
Angellll's solution worked.  Dynamic sql is my nemesis, don't make it yours too.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question