SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateTime error
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

godspeedba
Yak Posting Veteran

89 Posts

Posted - 09/01/2013 :  15:27:45  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

277 Posts

Posted - 09/01/2013 :  17:07:15  Show Profile  Reply with Quote
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)

Singapore
17440 Posts

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


KH
Time is always against us

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2170 Posts

Posted - 09/02/2013 :  01:06:03  Show Profile  Reply with Quote
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

Edited by - bandi on 09/02/2013 01:06:58
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000