上月末的日期:select last_day(add_months(sysdate, -1)) from dual; 本月的最后一秒:select trunc(add_months(sysdate,1),'MM') - 1/24/60/60 from dual 本周星期一的日期:select trunc(sysdate,'day')+1 from dual
年初至今的天數(shù):select ceil(sysdate - trunc(sysdate, 'year')) from dual;
今天是今年的第幾周 :select to_char(sysdate,'fmww') from dual 今天是本月的第幾周:SELECT TO_CHAR(SYSDATE,'WW') - TO_CHAR(TRUNC(SYSDATE,'MM'),'WW') + 1 AS "weekOfMon" FROM dual
本月的天數(shù) SELECT to_char(last_day(SYSDATE),'dd') days FROM dual 今年的天數(shù) select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual 下個星期一的日期 SELECT Next_day(SYSDATE,'monday') FROM dual
============================================
--計算工作日方法
create table t(s date,e date); alter session set nls_date_format = 'yyyy-mm-dd'; insert into t values('2003-03-01','2003-03-03'); insert into t values('2003-03-02','2003-03-03'); insert into t values('2003-03-07','2003-03-08'); insert into t values('2003-03-07','2003-03-09'); insert into t values('2003-03-05','2003-03-07'); insert into t values('2003-02-01','2003-03-31');
SELECT CASE WHEN to_number(to_char(SYSDATE,'hh24')) BETWEEN 6 AND 11 THEN '上午' WHEN to_number(to_char(SYSDATE,'hh24')) BETWEEN 11 AND 17 THEN '下午' WHEN to_number(to_char(SYSDATE,'hh24')) BETWEEN 17 AND 21 THEN '晚上' END FROM dual;
將數(shù)字轉換為任意時間格式.如秒:需要轉換為天/小時 SELECT to_char(floor(TRUNC(936000/(60*60))/24))||'天'||to_char(mod(TRUNC(936000/(60*60)),24))||'小時' FROM DUAL
TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal twelfth Month: mm number 03 mon abbreviated mar month spelled out march Year: yy two digits 98 yyyy four digits 1998
2. select to_char( to_date(222,'J'),'Jsp') from dual
顯示Two Hundred Twenty-Two
3. 求某天是星期幾 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; 星期一 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; monday 設置日期語言 ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; 也可以這樣 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 兩個日期間的天數(shù) select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 時間為null的用法 select id, active_date from table1 UNION select 1, TO_DATE(null) from dual;
注意要用TO_DATE(null)
6. a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd') 那么12月31號中午12點之后和12月1號的12點之前是不包含在這個范圍之內(nèi)的。 所以,當時間需要精確的時候,覺得to_char還是必要的 7. 日期格式?jīng)_突問題 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01' alter system set NLS_DATE_LANGUAGE = American alter session set NLS_DATE_LANGUAGE = American 或者在to_date中寫 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; 注意我這只是舉了NLS_DATE_LANGUAGE,當然還有很多, 可查看 select * from nls_session_parameters select * from V$NLS_PARAMETERS
8. select count(*) from ( select rownum-1 rnum from all_objects where rownum = to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1 ) where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not in ( '1', '7' )
Monday-Sunday, for format code DAY Mon-Sun, for format code DY 1-7, for format code D
11 select to_char(sysdate,'hh:mi:ss') TIME from all_objects 注意:第一條記錄的TIME 與最后一行是一樣的 可以建立一個函數(shù)來處理這個問題 create or replace function sys_date return date is begin return sysdate; end;
select to_char(sys_date,'hh:mi:ss') from all_objects; 12. 獲得小時數(shù)
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer SQL> select sysdate ,to_char(sysdate,'hh') from dual;
將數(shù)字轉換為任意時間格式.如秒:需要轉換為天/小時 SELECT to_char(floor(TRUNC(936000/(60*60))/24))||'天'||to_char(mod(TRUNC(936000/(60*60)),24))||'小時' FROM DUAL
TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal twelfth Month: mm number 03 mon abbreviated mar month spelled out march Year: yy two digits 98 yyyy four digits 1998
2. select to_char( to_date(222,'J'),'Jsp') from dual
顯示Two Hundred Twenty-Two
3. 求某天是星期幾 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; 星期一 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; monday 設置日期語言 ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; 也可以這樣 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 兩個日期間的天數(shù) select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 時間為null的用法 select id, active_date from table1 UNION select 1, TO_DATE(null) from dual;
注意要用TO_DATE(null)
6. a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd') 那么12月31號中午12點之后和12月1號的12點之前是不包含在這個范圍之內(nèi)的。 所以,當時間需要精確的時候,覺得to_char還是必要的 7. 日期格式?jīng)_突問題 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01' alter system set NLS_DATE_LANGUAGE = American alter session set NLS_DATE_LANGUAGE = American 或者在to_date中寫 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; 注意我這只是舉了NLS_DATE_LANGUAGE,當然還有很多, 可查看 select * from nls_session_parameters select * from V$NLS_PARAMETERS
8. select count(*) from ( select rownum-1 rnum from all_objects where rownum = to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1 ) where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not in ( '1', '7' )
Monday-Sunday, for format code DAY Mon-Sun, for format code DY 1-7, for format code D
11 select to_char(sysdate,'hh:mi:ss') TIME from all_objects 注意:第一條記錄的TIME 與最后一行是一樣的 可以建立一個函數(shù)來處理這個問題 create or replace function sys_date return date is begin return sysdate; end;
select to_char(sys_date,'hh:mi:ss') from all_objects; 12. 獲得小時數(shù)
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer SQL> select sysdate ,to_char(sysdate,'hh') from dual;
將數(shù)字轉換為任意時間格式.如秒:需要轉換為天/小時 SELECT to_char(floor(TRUNC(936000/(60*60))/24))||'天'||to_char(mod(TRUNC(936000/(60*60)),24))||'小時' FROM DUAL
TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal twelfth Month: mm number 03 mon abbreviated mar month spelled out march Year: yy two digits 98 yyyy four digits 1998
2. select to_char( to_date(222,'J'),'Jsp') from dual
顯示Two Hundred Twenty-Two
3. 求某天是星期幾 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; 星期一 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; monday 設置日期語言 ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; 也可以這樣 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 兩個日期間的天數(shù) select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 時間為null的用法 select id, active_date from table1 UNION select 1, TO_DATE(null) from dual;
注意要用TO_DATE(null)
6. a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd') 那么12月31號中午12點之后和12月1號的12點之前是不包含在這個范圍之內(nèi)的。 所以,當時間需要精確的時候,覺得to_char還是必要的 7. 日期格式?jīng)_突問題 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01' alter system set NLS_DATE_LANGUAGE = American alter session set NLS_DATE_LANGUAGE = American 或者在to_date中寫 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; 注意我這只是舉了NLS_DATE_LANGUAGE,當然還有很多, 可查看 select * from nls_session_parameters select * from V$NLS_PARAMETERS
8. select count(*) from ( select rownum-1 rnum from all_objects where rownum = to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1 ) where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not in ( '1', '7' )
Monday-Sunday, for format code DAY Mon-Sun, for format code DY 1-7, for format code D
11 select to_char(sysdate,'hh:mi:ss') TIME from all_objects 注意:第一條記錄的TIME 與最后一行是一樣的 可以建立一個函數(shù)來處理這個問題 create or replace function sys_date return date is begin return sysdate; end;
select to_char(sys_date,'hh:mi:ss') from all_objects; 12. 獲得小時數(shù)
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer SQL> select sysdate ,to_char(sysdate,'hh') from dual;
將數(shù)字轉換為任意時間格式.如秒:需要轉換為天/小時 SELECT to_char(floor(TRUNC(936000/(60*60))/24))||'天'||to_char(mod(TRUNC(936000/(60*60)),24))||'小時' FROM DUAL
TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal twelfth Month: mm number 03 mon abbreviated mar month spelled out march Year: yy two digits 98 yyyy four digits 1998
2. select to_char( to_date(222,'J'),'Jsp') from dual
顯示Two Hundred Twenty-Two
3. 求某天是星期幾 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; 星期一 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; monday 設置日期語言 ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; 也可以這樣 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 兩個日期間的天數(shù) select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 時間為null的用法 select id, active_date from table1 UNION select 1, TO_DATE(null) from dual;
注意要用TO_DATE(null)
6. a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd') 那么12月31號中午12點之后和12月1號的12點之前是不包含在這個范圍之內(nèi)的。 所以,當時間需要精確的時候,覺得to_char還是必要的 7. 日期格式?jīng)_突問題 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01' alter system set NLS_DATE_LANGUAGE = American alter session set NLS_DATE_LANGUAGE = American 或者在to_date中寫 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; 注意我這只是舉了NLS_DATE_LANGUAGE,當然還有很多, 可查看 select * from nls_session_parameters select * from V$NLS_PARAMETERS
8. select count(*) from ( select rownum-1 rnum from all_objects where rownum = to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1 ) where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not in ( '1', '7' )
Monday-Sunday, for format code DAY Mon-Sun, for format code DY 1-7, for format code D
11 select to_char(sysdate,'hh:mi:ss') TIME from all_objects 注意:第一條記錄的TIME 與最后一行是一樣的 可以建立一個函數(shù)來處理這個問題 create or replace function sys_date return date is begin return sysdate; end;
select to_char(sys_date,'hh:mi:ss') from all_objects; 12. 獲得小時數(shù)
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer SQL> select sysdate ,to_char(sysdate,'hh') from dual;