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
 DateTime error

Author  Topic 

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2013-09-01 : 15:27:45
DECLARE @datefrom DateTime
SET @datefrom = '30/11/2013 00:00:00'



SELECT [events].datefrom, [events].*
FROM [events]
WHERE [events].live > 0 AND [events].deleted < 1
AND (@datefrom <= datefrom OR @datefrom is null OR @datefrom = '')

WHEN running this in SQL, this flashes a list of the events with the date filter then reports:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

(852 row(s) affected).

I'm confused, I don't know what is going on?
I got the '30/11/2013 00:00:00' from .net datetime SQL parameter type

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2013-09-01 : 17:07:15
Use:
SET @datefrom = convert(datetime,'2013-11-30 00:00:00',120)

or:
SET @datefrom = cast('2013-11-30 00:00:00' as datetime)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-09-01 : 21:59:55
best is simply specify the date in ISO format
SET @datefrom = '2013-11-30'


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-09-02 : 01:06:03
Better to use ISO format for DATE..

DECLARE @datefrom DateTime
SET @datefrom = '20131130' -- YYYYMMDD
SELECT @datefrom
what is the datatype of column datefrom ?
If VARCHAR, first convert datefrom to datetime in the WHERE clause

Refer this link: http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html
--
Chandu
Go to Top of Page
   

- Advertisement -