views:

87

answers:

8

I have 10 rows for today's date but my select statement based on date dosen't seem to work....

SELECT Id,WirelessId,RegNo,DriverName1,MobileNo1,DriverName2,MobileNo1 from
 DailySchedule where IsDeleted=0 and CreatedDate='2010-05-28'

Any suggestion...

A: 

Assuming that CreatedDate is a datetime or a date, I don't see what's wrong with the syntax of the where clause. Maybe the IsDeleted field for those columns is actually NULL? (Or non-zero, at any rate.)

Edit: what Dave says, or DATE(CreatedDate) = '2010-05-28'

Frank Shearar
+7  A: 

Only if the dates assigned to them are midnight today. It might be better to do:

CreatedDate BETWEEN '2010-05-28 00:00' AND '2010-05-29 00:00'
Dave
A safer format is to have a 'T' between the date and time, instead of a space character. (Safer in terms of SQL server always gets the conversion right, no matter what locale/datetime settings are applied, and where)
Damien_The_Unbeliever
You will miss enty entries in the last minute, take a look at http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-does-between-work-with-dates-in-sql-
SQLMenace
@Dave it worked thank you...
Priyaa Muthuraman
@SQLMenace fixed
Dave
That is not fixed either since now you will get the entries at exactly midnight. A better way would be and CreatedDate >='20100528'and CreatedDate < '20100529' also your dateformat is not safe, use ISO (YYYYMMDD) formats
SQLMenace
A: 

I'd check to see the type of the CreatedDate column and make sure that the string you're passing is being converted to a date properly.

When you say you 'have' 10 rows, is that confirmed visually or with a query executed in a console?

duffymo
@duffymo query executed in a consule without where clause
Priyaa Muthuraman
A: 

Not enough info to go on but I suspect that your dates have a time component and so don't match the string exactly.

Try using:

datediff(day, CreatedDate, '28-may-2010')) = 0
Martin
That is non SARGable, the optimizer needs to do a scan
SQLMenace
A: 
SELECT Id,WirelessId,RegNo,DriverName1,MobileNo1,DriverName2,MobileNo1 from
 DailySchedule where IsDeleted=0 and date_format(CreatedDate, "%Y-%m-%d")='2010-05-28'
Salil
+1  A: 

If you want all the entries for May 28th I would do

and CreatedDate >='20100528'
and CreatedDate < '20100529'

Notice the safe ISO format (YYYYMMDD) no dashes

Also take a look at How Does Between Work With Dates In SQL Server? to see why between can not give you all the results you want

SQLMenace
A: 

Can you post the results, or part of them, without where clause?

pcent
A: 

This shud work too !

SELECT Id,WirelessId,RegNo,DriverName1,MobileNo1,DriverName2,MobileNo1 from 
 DailySchedule where IsDeleted=0 and CONVERT(varchar,CreatedDate,101) ='05/28/2010' 
Baaju