資料庫日期查詢sql小筆記

2021-10-23 11:27:19 字數 2006 閱讀 4658

1、查詢當前時間

select sysdate from dual;

2、當前日期

select to_date(sysdate) from dual;(日期格式)

select to_char(to_date(sysdate),『yyyy-mm-dd』 ) from dual;(字串)

3、當前月第一天

select trunc(sysdate, 『mm』) from dual;(日期格式)

select to_char(trunc(sysdate, 『mm』),『yyyy-mm-dd』) from dual;(字串)

4、當前月最後一天

select to_date(last_day(sysdate))from dual;(日期格式)

select to_char(last_day(sysdate),『yyyy-mm-dd』) from dual;(字串)

5、當前周第一天

select trunc(sysdate, 『d』) from dual;(日期格式)

select to_char(trunc(sysdate, 『d』),『yyyy-mm-dd』) from dual;(字串)

6、當前周最後一天

select(trunc(sysdate, 『d』))+7 from dual;(日期格式)

select to_char((trunc(sysdate, 『d』))+7,『yyyy-mm-dd』) from dual;(字串)

7、上月第一天

select add_months(trunc(sysdate,『mm』),-1) from dual;(日期格式)

select to_char(add_months(trunc(sysdate,『mm』),-1),『yyyy-mm-dd』) from dual;(字串)

8、上月最後一天

select to_date(add_months(last_day(sysdate),-1)) from dual;(日期)

select to_char(add_months(last_day(sysdate),-1),『yyyy-mm-dd』) from dual;(字串)

9、下月當前日期

select add_months(sysdate,1) from dual;

10、上月當前日期

select add_months(sysdate,-1) from dual;

11、去年當前日期

select add_months(sysdate,-12) from dual;

12、明年當前日期

select add_months(sysdate,12) from dual;

13、去年本月第一天

select add_months(trunc(sysdate,『mm』),-12) from dual;

select to_char(add_months(trunc(sysdate,『mm』),-12),『yyyy-mm-dd』) from dual;

14、去年本月最後一天

select to_date(add_months(last_day(sysdate),-12)) from dual;

select to_char(add_months(last_day(sysdate),-12),『yyyy-mm-dd』) from dual;

15、下年本月第一天

select add_months(trunc(sysdate,『mm』),12) from dual;

select to_char(add_months(trunc(sysdate,『mm』),12),『yyyy-mm-dd』) from dual;

16、下年本月最後一天

select to_date(add_months(last_day(sysdate),12)) from dual;

select to_char(add_months(last_day(sysdate),12),『yyyy-mm-dd』) from dual;

資料庫日期查詢

有表a,日期字段,週期字段 週期欄位為一年,兩年,三年 現要查詢 當前時間是否大於 日期字段 oracle 中執行sql 語句如下 ds cycle 週期字段,為字典值,配置項,值為月份 一年12個月 select from t relay protection t where add months...

資料庫日期範圍查詢

面試遇到了日期範圍的問題,問題基本很簡單,記錄下 查詢在三十天內建立的所有人員 select from tablename t where to char t.create date,yyyymmdd to char sysdate 30,yyyymmdd 獲得當前日期的前三個月 select ad...

資料庫中SQL查詢指定日期內資料

mysql資料庫 有時我們面要統計一下資料庫中指定日期的資料,如常用的,今天,昨天,明天,本週 上週 本月 上個月份的資料,除了我們可以使用strtotime來算還可以參考下面方法。今天select from 表名 where to days 時間欄位名 to days now 昨天 select ...