?
Solved

Dynamic SQL with DateTime conversion

Posted on 2008-10-09
9
Medium Priority
?
728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 143

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 143

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

800 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