简体   繁体   English

这个sql语句是否正确?

[英]Is this sql where statement correct?

I have 10 rows for today's date but my select statement based on date dosen't seem to work.... 我今天的日期有10行,但基于日期的选择语句似乎不起作用....

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

Any suggestion... 任何建议......

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'

If you want all the entries for May 28th I would do 如果你想要5月28日的所有参赛作品,我会这么做

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

Notice the safe ISO format (YYYYMMDD) no dashes 注意安全的ISO格式(YYYYMMDD)没有破折号

Also take a look at How Does Between Work With Dates In SQL Server? 另请参阅SQL Server如何使用日期? to see why between can not give you all the results you want 看看为什么之间不能给你想要的所有结果

Assuming that CreatedDate is a datetime or a date, I don't see what's wrong with the syntax of the where clause. 假设CreatedDate是日期时间或日期,我看不出where子句的语法有什么问题。 Maybe the IsDeleted field for those columns is actually NULL? 也许这些列的IsDeleted字段实际上是NULL? (Or non-zero, at any rate.) (无论如何,或者非零。)

Edit: what Dave says, or DATE(CreatedDate) = '2010-05-28' 编辑:Dave说的话,或DATE(CreatedDate)='2010-05-28'

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. 我将检查CreatedDate列的类型,并确保正在传递的字符串正确转换为日期。

When you say you 'have' 10 rows, is that confirmed visually or with a query executed in a console? 当你说'有'10行时,是在视觉上确认还是在控制台中执行查询?

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
SELECT Id,WirelessId,RegNo,DriverName1,MobileNo1,DriverName2,MobileNo1 from
 DailySchedule where IsDeleted=0 and date_format(CreatedDate, "%Y-%m-%d")='2010-05-28'

This should work too 这应该也有效

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM