SQL 判斷時間條件

2022-08-24 16:03:10 字數 1247 閱讀 4372

1.判斷年月日是否相等

select

*from

table

where

cast(convert(varchar(10), 時間字段, 120) as

datetime)=

'2010-10-01

'

判斷邏輯:通過convert函式現將時間欄位中年月日擷取出來,然後利用cast函式轉化為datetime。

2.判讀度分秒是否相等

set

@transfrom_date

=cast('

1900-1-1 '+

convert(varchar(100), '

時間字段

', 108) as

datetime

)select

*from

table

where

cast('

1900-1-1 '+

convert(varchar(100), '

時間字段

', 108) as

datetime)=

@transfrom_date

判斷邏輯:現將時間條件語句轉化為度分秒,然後執行查詢並將比對的時間字段轉化為同一年月日的度分秒。

3.判斷是否為同一年份、月份、日

select

*from

table

--年份

where

datediff(year,'

時間字段

','時間條件

')=0

--月份

where

datediff(month,'

時間字段

','時間條件

')=0

--日where datediff(day,'時間字段','時間條件')=0

4.判斷未來多少天即將過生日的人員資訊

select

*from

table

where

0<=

datediff(dd,getdate(),dateadd(yy,datediff(yy, birthday, getdate

()),birthday))

anddatediff(dd,getdate(),dateadd(yy,datediff(yy, birthday, getdate()),birthday))<=天數

sql 條件判斷

case具有兩種格式。分支結構case語句和條件判斷case語句。分支結構case語句,類似於switch select case user name when 張三 then zhangsan when 李四 then lisi when 王五1 then wangwu else 找不到對應 en...

sql 時間條件查詢

select from table t where t.time to date aaaa,yyyy mm dd hh24 mm ss and t.timeaaaa,bbbb是字串型別 比如 aaaa 2018 04 19 00 00 00 bbbb 2018 04 20 00 00 00 to d...

sql 時間條件查詢

select from table t where t.time to date aaaa,yyyy mm dd hh24 mm ss and t.timeaaaa,bbbb是字串型別 比如 aaaa 2018 04 19 00 00 00 bbbb 2018 04 20 00 00 00 to d...