SQL SERVER查询时间条件式写法 | 您所在的位置:网站首页 › sqlserver时间约束 › SQL SERVER查询时间条件式写法 |
如下表: tid tname tdate 1 张三 2009-10-29 19:04:50 2 李四 2006-11-12 11:04:50 3 王五 2007-6-11 4 陈六 2007-8-26 12:04:50 5 小二 2009-11-28 下面开始做各种时间查询:注意:避免在WHERE条件式中对字段做运算,比如convert(char(10),tdate,120) select tname from tt Where tdate='2007-06-11' --当时间段值为2007-06-11 00:00:00.000时,此条件式成立 --select tname 时间值没有 from tt Where tdate='2007-08-26' --时间段值为2007-08-26 12:04:50.000,此条件式不成立,时间值不同,这样找不到当天值 select tname 查找当天时间 from tt Where tdate between '2007-08-26 00:00:00' and '2007-08-26 23:59:59' --select tname 今天的值 from tt Where tdate =getdate() --不行,因为SQL会默认当天起点是00:00:00开始, select tname 今天的值方式一 from tt Where tdate between convert(char(10),getdate(),120) and convert(char(10),getdate(),120)+' 23:59:59' select tname 今天的值方式二 from tt Where tdate >= convert(char(10),getdate(),120) and tdate='2007-06-11' and tdate |
CopyRight 2018-2019 实验室设备网 版权所有 |