SQL讀取當天的數據
1、SQL在查詢當天記錄時要注意是從當天的0點0分0秒0毫秒開始,到次日0點0分0秒0毫秒截止,但不包含次日的0點0分0秒0毫秒。
2、注意:在不同數據庫產品中,獲得當天日期的函數不一樣。
MSSQL獲得當前日期:
convert(varchar(10),Getdate(),120)
MYSQL獲得當前日期:
date(now())
Oracle獲得當前日期:
to_char(sysdate,'yyyy-mm-dd')
Access獲得當前日期:
date()
3、在各個數據庫里獲得當天的記錄寫法為(假設表名為:Table_1,日期列名為:date_col):
MSSQL獲得當天記錄:
select * from table_1 where date_col>=convert(varchar(10),Getdate(),120) and date_col<convert(varchar(10),dateadd(d,1,Getdate()),120)
MYSQL獲得當天記錄:
select * from table_1 where date_col>=date(now()) and date_col<DATE_ADD(date(now()),INTERVAL 1 DAY)
Oracle獲得當天記錄:
select * from table_1 where date_col>=to_char(sysdate,'yyyy-mm-dd') and date_col<to_char(sysdate+1,'yyyy-mm-dd')
Access獲得當天記錄:
select * from table_1 where date_col>=date() and date_col<DateAdd("d",1,date())
4、另外,在查詢的時候,盡量不要對列進行運算,因為日期列上若有索引,就無法使用索引了。