教您SQL SERVER查询时间条件式写法 |
本文标签:SQL SERVER查询 查询时SQL SERVER数据库的核心,下面就将教您SQL SERVER查询时间条件式写法,供您参考,希望对您更好学习SQL SERVER查询能够有所帮助 。 如下表: tid tname tdate 下面开始做各种时间查询:注意:避免在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 convert(char(10),getdate(),120) and convert(char(10),getdate(),120)+ 23:59:59 --select tname from tt Where left(tdate,10)=2007-06-11 -- --select tname from tt Where convert(char(10),tdate,120)=2007-06-11 --查找当天值,可以用,但效率低 --select tname aa from tt Where convert(char(10),tdate,120)=2007-08-26 --查找当天值,可以用,但如果条件式后面为‘2007-8-26’则不行.因为char(10)限定十位 --select tname 时间 from tt Where tdate Like 2007-08-26% --不可用,找不到记录 查找时间段也是一样的,如: select tname 查找2007年6月11号到8月26的记录 From tt Where tdate >=2007-06-11 and tdate<= 2007-08-26 23:59:59 以上就是SQL SERVER查询时间条件式写法 。 |