views:

97

answers:

6
SELECT [NEXT_UPDATE_TIME] 
FROM [BGREX_UpdateQueue]
WHERE [NEXT_UPDATE_TIME] <  18/12/2009

I want to select those dates from the records where datetime value in column is less than some datetime, we need to compare. I have several value lass than "18/12/2009", but getting no rows returned.

following syntax does not work either

SELECT 18/12/2009 // returns 0 
WHERE [NEXT_UPDATE_TIME] < '18/12/2009'; // The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

What is the right syntax to pass '18/12/2009' or some other date ? thanks

+2  A: 

Try:

select next_update_time from bgrex_updatequeue
  where next_update_time < '2009-12-18';
Jim H.
already tried this, gives this error---The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Asad Butt
No, you tried '18/12/2009'. '2009-12-18' is a different expression entirely, and one that should convert correctly.
Joel Coehoorn
Thanks mate, was my fault, works fine
Asad Butt
A: 

This should work :

SELECT [NEXT_UPDATE_TIME] 
FROM [BGREX_UpdateQueue]
WHERE [NEXT_UPDATE_TIME] < '2009-12-18'

By the way you're getting the The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. error because of the date's format, if you try 12/18/2009, it should work also, but the query above is the best one I think.

Canavar
A: 

DATEDIFF is your friend in this case.

Jay Zeng
I don't mind a down vote, but you might want to let me know what I've gotten wrong. I am always happy to learn more when I am mistaken.
Jay Zeng
A: 

If you are using a DataView, it can be accomplished easily doing this (for example between dates):

myDataView = new DataView(myDataSet.Tables["TABLE"], "TIMESTAMP >= '" + Convert.ToDateTime(fromDate) + "' AND TIMESTAMP <= '" + Convert.ToDateTime(toDate) + "'", "TIMESTAMP", DataViewRowState.CurrentRows);

toDate and fromDate are of type string

There is also an article on Working with SQL Server Date/Time Variables: Part Three - Searching for Particular Date Values and Ranges

0A0D
A: 

The query in your example is missing quotes around the date, it should be:

SELECT [NEXT_UPDATE_TIME] 
FROM [BGREX_UpdateQueue]
WHERE [NEXT_UPDATE_TIME] <  '18/12/2009'
Rory
same as @ angry jim
Asad Butt
@asdi: then use a date format that your server expects, 'dd MMM yyyy' always works ('18 Dec 2009')
Rory
thanks it was suppose to be '2009-12-18'
Asad Butt
+2  A: 

try universal SQL Date Format YYYYMMDD

WHERE [NEXT_UPDATE_TIME] < '20091218'

see standard SQL datetime formats

Charles Bretana