新聞中心
查詢時SQL SERVER數(shù)據(jù)庫的核心,下面就將教您SQL SERVER查詢時間條件式寫法,供您參考,希望對您更好學(xué)習(xí)SQL SERVER查詢能夠有所幫助。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供喀喇沁企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站制作、HTML5建站、小程序制作等業(yè)務(wù)。10年已為喀喇沁眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
如下表:
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條件式中對字段做運(yùn)算,比如convert(char(10),tdate,120)
select tname from tt Where tdate='2007-06-11' --當(dāng)時間段值為2007-06-11 00:00:00.000時,此條件式成立
--select tname 時間值沒有 from tt Where tdate='2007-08-26' --時間段值為2007-08-26 12:04:50.000,此條件式不成立,時間值不同,這樣找不到當(dāng)天值
select tname 查找當(dāng)天時間 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() --不行,因?yàn)镾QL會默認(rèn)當(dāng)天起點(diǎn)是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 left(tdate,10)='2007-06-11' -- --select tname from tt Where convert(char(10),tdate,120)='2007-06-11' --查找當(dāng)天值,可以用,但效率低 --select tname aa from tt Where convert(char(10),tdate,120)='2007-08-26' --查找當(dāng)天值,可以用,但如果條件式后面為‘2007-8-26’則不行.因?yàn)閏har(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查詢時間條件式寫法。 【編輯推薦】 初級sql select語法 sql server死鎖的檢測方法 SQL SERVER字段類型的說明 SQL Server安全解析 保護(hù)sql server安全的措施
select tname 今天的值方式二 from tt Where tdate >= convert(char(10),getdate(),120) and tdate
--select tname 時間段 from tt Where tdate between '2007-06-11' and '2007-09-12'
分享題目:教您SQLSERVER查詢時間條件式寫法
瀏覽路徑:http://m.fisionsoft.com.cn/article/cdseipe.html


咨詢
建站咨詢
