Oracle学习笔记系列(⼆)之数据库⽇期格式转换
Oracle数据库⽇期格式转换
select sysdate from dual;select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') as mydate from dual;select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as mydate from dual;select to_number(to_char(sysdate,'yyyymmddhh24miss')) as mydate from dual;
转换函数
与date操作关系最⼤的就是两个转换函数:to_date(),to_char() to_date():作⽤将字符类型按⼀定格式转化为⽇期类型。
具体⽤法:to_date('2004-11-27','yyyy-mm-dd'),前者为字符串,后者为转换⽇期格式。【注意,前后两者要以⼀对应】
如;to_date('2004-11-27 13:34:43', 'yyyy-mm-dd hh24:mi:ss')
多种⽇期格式:
YYYY:四位表⽰的年份 YYY,YY,Y:年份的最后三位、两位或⼀位,缺省为当前世纪 MM:01~12的⽉份编号 MONTH:九个字符表⽰的⽉份,右边⽤空格填补 MON:三位字符的⽉份缩写 WW:⼀年中的星期 D 的第⼏个⼩时,取值为00~23 MI:⼀⼩时中的分钟 SS:⼀分钟中的秒 SSSS:从午夜开始过去的秒数
to_char():将⽇期转按⼀定格式换成字符类型即把当前时间按yyyy-mm-dd hh24:mi:ss格式转换成字符类型在oracle中处理⽇期⼤全 TO_DATE格式 Day:dd number 12dy abbreviated friday spelled out fridayddspth sp 12⼩时格式下时间范围为: 1:00:00 - 12:59:59 ....
⽇期和时间函数汇总
1.⽇期和字符转换函数⽤法(to_date,to_char)
select to_char(to_date(222,'J'),'Jsp') from dual; --Two Hundred Twenty-Two下雨的日子
2.求某天是星期⼏
select to_char(to_date('2018-01-09','yyyy-mm-dd'),'day') from dual; --星期⼆
select to_char(to_date('2018-01-09','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; --tuesday
设置⽇期语⾔ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以这样 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
3.两⽇期间的天数
select floor(sysdate - to_date('19921123','yyyymmdd')) from dual; --9179
亲爱的小冤家歌曲4. 时间为null的⽤法
select p.claimno, p.endcasedate from prplclaim pUNION
select '1', TO_DATE(null) from dual;
注意要⽤TO_DATE(null)
5.取⽇期范围
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
这样12⽉31号中午12点之后和12⽉1号的12点之前是不包含在这个范围之内的。因此当时间需要精确
黄旭熙宋雨琦恋爱的时候,to_char更好。  6. ⽇期格式冲突问题输⼊的格式要看你安装的ORACLE字符集的类型, ⽐如: US7ASC to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; 注意我这只是举了NLS_DATE_LANGUAGE,当然还有很多,可查看 select * from nls_session_parametersselect 7.查2002-02-28⾄2002-02-01间除星期⼀和七的天数在前后分别调⽤DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, ⽽不是毫秒).
select count(*)from ( select rownum-1 rnumfrom all_objectswhere 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' )
8.时间间隔
select months_between(to_date('01-31-1999','MM-DD-YYYY'), to_date('12-31-1998','MM-DD-YYYY')) 'MONTHS' FROM DUAL; --1
select months_between(to_date('02-01-1999','MM-DD-YYYY'), to_date('12-31-1998','MM-DD-YYYY')) 'MONTHS' FROM DUAL; --1.03225806451613
9.获得⼩时数
select sysdate ,to_char(sysdate,'hh') from dual;select sysdate ,to_char(sysdate,'hh24') from dual;
获取年⽉⽇与此类似
10.处理⽉份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual;
11.出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual;
闰年的处理⽅法
to_char( last_day( to_date('02' || :year,'mmyyyy') ), 'dd' )  --如果是28就不是闰年
12.不同时区的处理
select to_char(NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate from dual;
13.5秒钟⼀个间隔七百年后
select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')from dual; --SSSSS表⽰5位秒数
14.⼀年的第⼏天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual;
15.计算⼩时,分,秒,毫秒
selectDays,
A,
TRUNC(A*24) Hours,
TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,
TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,
TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSecondsfrom(selecttrunc(sysdate) Days,
sysdate - trunc(sysdate) Afrom dual);
floor((date2-date1) /365) 作为年 floor((date2-date1, 365) /30) 作为⽉ mod(mod(date2-date1, 365), 30)作为⽇.
关于⽇期的函数在oracle中有很多关于⽇期的函数,如: 1、add_months()⽤于从⼀个⽇期值增加或减少⼀些⽉份 date_value:=add_months(date_value,number_of_months) 例:
select add_months(sysdate,12) 'Next Year' from dual;
2、current_date()返回当前会放时区中的当前⽇期 date_value:=current_date
select sessiontimezone,current_date from dual;
3、current_timestamp()以timestamp with time zone数据类型返回当前会放时区中的当前⽇期 timestamp_with_time_zone_value:=current_timestamp([timestamp_precision])
select sessiontimezone,current_timestamp from dual;
4、dbtimezone()返回时区
select dbtimezone from dual;
5、extract()出⽇期或间隔值的字段值
select extract(month from sysdate) 'This Month' from dual;select extract(year from add_months(sysdate,36)) '3 Years Out' from dual;
6、last_day()返回包含了⽇期参数的⽉份的最后⼀天的⽇期 date_value:=last_day(date_value)
select last_day(date'2000-02-01') 'Leap Yr?' from dual;select last_day(sysdate) 'Last day of this month' from dual;
7、localtimestamp()返回会话中的⽇期和时间
select localtimestamp,current_timestamp from dual;
8、months_between()判断两个⽇期之间的⽉份数量
select months_between(sysdate,date'1971-05-18') from dual;
9、next_day()给定⼀个⽇期值,返回由第⼆个参数指出的⽇⼦第⼀次出现在的⽇期值(应返回相应⽇
⼦的名称字符串)
next_day(sysdate,6)是从当前开始下⼀个星期五。后⾯的数字是从星期⽇开始算起。 1 2 3 4 5 6 7 ⽇⼀⼆三四五六10、周相隔⽇期函数 trunc() 1.查询某周的第⼀天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') last_dayfrom (select substr('2004-32', 1, 4) yy, to_number(substr('2004-32', 6)) ww from dual);select trunc(to_date(substr('2003-01',1,5)||to_char((to_nu 2.查询某周的最后⼀天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') - 6 first_dayfrom (select substr('2004-33', 1, 4) yy, to_number(substr('2004-33', 6)) ww from dual);select trunc(to_date(substr('2003-01',1,5)||to_char((to 3.查询某周的⽇期
马天宇的歌
select min_date, to_char(min_date,'day') day from(select to_date(substr('2004-33',1,4)||'001'+rownum-1,'yyyyddd') min_datefrom all_tableswhere 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_datefrom all_tableswhere rownum <= 7
天空之城小提琴谱
union
select to_date(substr('2004-33',1,4)+1||'001'+rownum-1,'yyyyddd') min_datefrom all_tableswhere rownum <= 7)where to_char(min_date,'yyyy-iw') ='2004-33';