to_char():将日期转按一定格式换成字符类型 SQL> select to_char(sysdate,''yyyy-mm-dd hh24:mi:ss'') time from dual;
TIME ------------------- 2004-10-08 15:22:58
即把当前时间按yyyy-mm-dd hh24:mi:ss格式转换成字符类型
在oracle中处理日期大全
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. 两个日期间的天数 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点之前是不包含在这个范围之内的。 所以,当时间需要精确的时候,觉得to_char还是必要的 7. 日期格式冲突问题 输入的格式要看你安装的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 与最后一行是一样的 可以建立一个函数来处理这个问题 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. 获得小时数
SELECT EXTRACT(HOUR FROM TIMESTAMP ''2001-02-16 2:38:40'') from offer SQL> select sysdate ,to_char(sysdate,''hh'') from dual;
3、current_timestamp()以timestamp with time zone数据类型返回当前会放时区中的当前日期 timestamp_with_time_zone_value:=current_timestamp([timestamp_precision]) SQL> column sessiontimezone for a15 SQL> column current_timestamp format a36 SQL> select sessiontimezone,current_timestamp from dual;
4、dbtimezone()返回时区 varchar_value:=dbtimezone SQL> select dbtimezone from dual;
DBTIME ------ -07:00
SQL>
5、extract()找出日期或间隔值的字段值 date_value:=extract(date_field from [datetime_value|interval_value]) SQL> select extract(month from sysdate) "This Month" from dual;
This Month ---------- 11
SQL> select extract(year from add_months(sysdate,36)) "3 Years Out" from dual;
3 Years Out ----------- 2006
SQL>
6、last_day()返回包含了日期参数的月份的最后一天的日期 date_value:=last_day(date_value) SQL> select last_day(date''2000-02-01'') "Leap Yr?" from dual;
Leap Yr? ---------- 29-2月 -00
SQL> select last_day(sysdate) "Last day of this month" from dual;
Last day o ---------- 30-11月-03
SQL>
7、localtimestamp()返回会话中的日期和时间 timestamp_value:=localtimestamp SQL> column localtimestamp format a28 SQL> select localtimestamp from dual;
select trunc(to_date(substr(''2003-01'',1,5)||to_char((to_number(substr(''2003-01'',6)))*7),''yyyy-ddd''),''d'')-6 first_day from dual
select min(v_date) from (select (to_date(''200201'',''yyyymm'') + rownum) v_date from all_tables where rownum < 370) where to_char(v_date,''yyyy-iw'') = ''2002-49''
select trunc(to_date(substr(''2003-01'',1,5)||to_char((to_number(substr(''2003-01'',6)))*7),''yyyy-ddd''),''d'') last_day from dual
select max(v_date) from (select (to_date(''200408'',''yyyymm'') + rownum) v_date from all_tables where rownum < 370) where to_char(v_date,''yyyy-iw'') = ''2004-33''
3.查询某周的日期 select min_date, to_char(min_date,''day'') day from (select to_date(substr(''2004-33'',1,4)||''001''+rownum-1,''yyyyddd'') min_date from all_tables where rownum <= decode(mod(to_number(substr(''2004-33'',1,4)),4),0,366,365) union
select to_date(substr(''2004-33'',1,4)-1|| decode(mod(to_number(substr(''2004-33'',1,4))-1,4),0,359,358)+rownum,''yyyyddd'') min_date from all_tables where rownum <= 7 union
select to_date(substr(''2004-33'',1,4)+1||''001''+rownum-1,''yyyyddd'') min_date from all_tables where rownum <= 7 ) where to_char(min_date,''yyyy-iw'') =''2004-33''