数据A表中date 字段是datetime类型的内容写进表的时候都是这样的格式'2009-4-18 10:30' 就是日期加时间当我在查询的时候
select * from a where date='2009-4-18'
居然'2009-4-18 10:30'这种记录查不到,一定要
select * from a where date='2009-4-18 10:30'这样才行,
这样的有没有简单办法解决呀,
select * from a where date='2009-4-18'
居然'2009-4-18 10:30'这种记录查不到,一定要
select * from a where date='2009-4-18 10:30'这样才行,
这样的有没有简单办法解决呀,
select * from a where datediff(day,date,'2009-4-18')=0
'2009-4-18'不等于'2009-4-18 10:30' 嘛, 因为'2009-4-18'的意思是18号零点零分'2009-4-18 00:00'有几种方法
1.
select * from a where date >= '2009-4-18' and date < dateadd( day , 1, '2009-4-18' )2.
select * from a where date between '2009-4-18' and '2009-4-18 23:59:59'3.
select * from a where datediff( day , [day], '2009-4-18' ) = 01, '2009-4-18' )
...
...
...当然 前两种写法的执行效率较高
select * from where date like '2009-4-18%' 如果是日期时间型,用一楼做法:
select * from a where datediff(day,date,'2009-4-18')=0
SQL codeselect * from a where datediff(day,date,'2009-4-18')=0
select * from a where date='2009-4-18 00:00:00'所以比较不出来我以前是和韩国人写SQL的方法和7楼的都一样,第二重方法用的最多。
这个实际上是查找 日期为 2009-4-18 00:00的
用DateDiff就行了
不过函数用多了可能会慢。如果时间不要求太详细,可以把字段改成DATE型的。
select * from a where datediff(day,[day],'2009-4-18' ) = 01, '2009-4-18' ) --where中的列使用函数,就不能用索引了select * from a where date >= '2009-4-18' and date < dateadd( day , 1, '2009-4-18' ) --还是直接用列效率高