查詢本日的記錄
select * from tablename where datepart(dd, thedate) = datepart(dd, getdate()) and datepart(mm, thedate) = datepart(mm, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
查詢本月的記錄
select * from tablename where datepart(mm, thedate) = datepart(mm, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
查詢本週的記錄
select * from tablename where datepart(wk, thedate) = datepart(wk, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
查詢本季的記錄
select * from tablename where datepart(qq, thedate) = datepart(qq, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
查詢 n 天前的記錄
select * from tablename where datediff(day,thedate,getdate())=n
查詢 n 年前的記錄
select * from tablename where datediff(year,thedate,getdate())=n
查詢 n 月前的記錄
select * from tablename where datediff(month,thedate,getdate())=n
********************=
表名為:tablename
時間欄位名為:thedate
getdate()是獲得系統時間的函式。
********************=
datepart 函式說明
日期部分 縮寫
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms
datediff 函式說明
日期部分 縮寫
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
hour hh
minute mi, n
second ss, s
millisecond ms
SQL日期查詢
1.sql查詢今天的資料 比如今日新增 datediff 函式返回兩個日期之間的時間。select from customer where datediff now create time 0 2.查詢昨天的資料 select from customer where datediff now cre...
SQL查詢日期
sql查詢日期 今天的所有資料 select from 表名 where datediff dd,datetime型別字段,getdate 0 昨天的所有資料 select from 表名 where datediff dd,datetime型別字段,getdate 1 7天內的所有資料 selec...
sql查詢 日期格式
使用convert函式轉換 convert varchar 10 欄位名,轉換格式 比如 select user id,convert varchar 10 date,11 as date from tb user 轉換格式 0或100 month dd yyyy hh miampm 1 mm dd...