Solved

Dynamic SQL with DateTime conversion

Posted on 2008-10-09
9
701 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a linked  server to connect ot an Access Database. 26 46
Modifying SQL 2008/2012 PARTITIONS 3 54
create index to c1, c2 and c3 9 65
CREATE DATABASE ENCRYPTION KEY 1 56
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now