Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql 2005 Syntax Error converting datatime

Author  Topic 

NaughtyZute
Starting Member

3 Posts

Posted - 2013-05-07 : 13:13:41
Local variables:
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SET @StartDate = '2012-10-01'
SET @EndDate = '2013-02-01'

String concatention:
SET @strColumns='SELECT * FROM ' + @strTableName + ' WHERE ' + @strColumns + ' AND LOAD_DATE BETWEEN ' + @StartDate + ' AND ' + @EndDate

Error Message: Msg 241... Syntax error converting datetime from character string...

I've been away from SQL for awhile and not sure I've ever used SQL 2005. Thanks for any help provided. I suspect my problem is somewhere in those pesky single quotes.


Zute
<h5>p.s. If you don't like your life; change the way you're living.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-05-07 : 13:43:39
Add a CONVERT or CAST command for your date parameters in the SET @str. You'll need to convert those to a character data type.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2013-05-07 : 14:06:28
This is really not a good idea - you should not be building (concatenating) SQL like this as it opens you to a SQL injection attack. With that said, if you really want to do this - then you need to declare the variables as a character data type and use convert to specify the string format of the date you want.

I would use CHAR(8) and CONVERT(char(8), datevalue, 121) which will return the date in the format YYYYMMDD.

I would also recommend that you reconsider using BETWEEN for the date comparison. The column LOAD_DATE is a datetime data type - which includes the time portion and using BETWEEN which is inclusive will either include data you don't want - or exclude data you do want. For example:

BETWEEN '20120101' AND '20130201' will include anything where the LOAD_DATE is exactly '2013-02-01 00:00:00.000' but will not include anything greater than or equal to '2013-02-01 00:00:00.333'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-08 : 00:37:01
quote:
Originally posted by NaughtyZute

Local variables:
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SET @StartDate = '2012-10-01'
SET @EndDate = '2013-02-01'

String concatention:
SET @strColumns='SELECT * FROM ' + @strTableName + ' WHERE ' + @strColumns + ' AND LOAD_DATE BETWEEN ' + @StartDate + ' AND ' + @EndDate

Error Message: Msg 241... Syntax error converting datetime from character string...

I've been away from SQL for awhile and not sure I've ever used SQL 2005. Thanks for any help provided. I suspect my problem is somewhere in those pesky single quotes.


Zute
<h5>p.s. If you don't like your life; change the way you're living.


it has something to do with formats of date values you're passing

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-08 : 00:38:04
quote:
Originally posted by jeffw8713

This is really not a good idea - you should not be building (concatenating) SQL like this as it opens you to a SQL injection attack. With that said, if you really want to do this - then you need to declare the variables as a character data type and use convert to specify the string format of the date you want.

I would use CHAR(8) and CONVERT(char(8), datevalue, 121) which will return the date in the format YYYYMMDD.

I would also recommend that you reconsider using BETWEEN for the date comparison. The column LOAD_DATE is a datetime data type - which includes the time portion and using BETWEEN which is inclusive will either include data you don't want - or exclude data you do want. For example:

BETWEEN '20120101' AND '20130201' will include anything where the LOAD_DATE is exactly '2013-02-01 00:00:00.000' but will not include anything greater than or equal to '2013-02-01 00:00:00.333'.




Yes indeed
thats why i prefer using logic with >= and <



http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -