自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SQL SERVER查詢時(shí)間條件式的寫法

數(shù)據(jù)庫 SQL Server
SQL SERVER查詢時(shí)間條件式應(yīng)該如何寫呢?這是很多人都提到的問題。下文就將帶您尋找其中的答案,希望對(duì)您能有所幫助。

查詢是SQL SERVER的核心功能,下面就為您詳細(xì)介紹SQL SERVER查詢時(shí)間條件式的寫法,如果您對(duì)此方面感興趣的話,不妨一看。

如下表:

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

下面開始做各種時(shí)間查詢:注意:避免在WHERE條件式中對(duì)字段做運(yùn)算,比如convert(char(10),tdate,120)

select tname from tt Where tdate='2007-06-11' --當(dāng)時(shí)間段值為2007-06-11 00:00:00.000時(shí),此條件式成立

--select tname 時(shí)間值沒有 from tt Where tdate='2007-08-26' --時(shí)間段值為2007-08-26 12:04:50.000,此條件式不成立,時(shí)間值不同,這樣找不到當(dāng)天值
select tname 查找當(dāng)天時(shí)間 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會(huì)默認(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 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' --查找當(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 時(shí)間 from tt Where tdate Like '2007-08-26%' --不可用,找不到記錄
--select tname 時(shí)間段 from tt Where tdate between '2007-06-11' and '2007-09-12'

查找時(shí)間段也是一樣的,如:

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

以上就是SQL SERVER查詢時(shí)間條件式的寫法介紹。

 

 

【編輯推薦】

SQL Server查詢結(jié)果集中的常量

sql server查詢字段詳細(xì)信息

SQL Server FROM子句的語法

SQL Server字符串函數(shù)大全

SQL Server綁定連接

責(zé)任編輯:段燃 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-09-14 16:36:34

SQL SERVER查

2019-06-27 10:35:40

JavaScript條件式匹配條件

2010-09-24 19:23:51

SQL查詢時(shí)間段

2010-09-25 16:42:45

sql語句

2010-11-09 15:18:37

SQL Server多

2009-03-23 10:47:43

數(shù)據(jù)庫SQLLINQ

2010-10-21 11:10:57

SQL Server查

2010-07-06 09:39:20

SQL Server分

2010-10-21 14:27:35

SQL Server時(shí)

2010-09-10 08:54:02

2010-09-13 13:44:22

sql server外

2010-11-09 16:20:46

SQL Server死

2021-01-19 05:39:17

SQLServer變量

2010-09-28 11:28:40

SQL字段屬性

2011-04-06 13:38:11

SQL ServerSQL語句

2010-09-26 16:56:14

sql語句

2010-11-09 15:30:01

Sql server時(shí)

2010-10-21 12:16:11

SQL Server查

2010-07-12 17:12:48

SQL Server數(shù)

2021-08-17 10:39:54

SQL Server數(shù)據(jù)庫優(yōu)化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)