日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQL SERVER查询时间条件式写法

發布時間:2023/12/18 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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<convert(char(10),getdate()+1,120)

--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 時間段 from tt Where tdate between '2007-06-11' and '2007-09-12'

查找時間段也是一樣的,如:

select tname 查找2007年6月11號到8月26的記錄 From tt Where tdate >='2007-06-11' and tdate<= '2007-08-26 23:59:59'
?

以上就是SQL SERVER查詢時間條件式寫法。

轉載于:https://www.cnblogs.com/xiaohui1990/p/3418550.html

總結

以上是生活随笔為你收集整理的SQL SERVER查询时间条件式写法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。