SQL讀取當天的數據


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、另外,在查詢的時候,盡量不要對列進行運算,因為日期列上若有索引,就無法使用索引了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM