Oracle_日期型数据
以 2008-11-2113:45:25 为例:
| 格式 | 说明 | 结果 |
年 | yy | 两位年 | 08 |
yyy | 三位年 | 008 | |
yyyy | 四位年 | 2008 | |
月 | mm | 数字表示 | 11 |
mon | 字符集简写表示 | 11月,若是英文版,则显示nov | |
month | 字符集全写表示 | 11月,若是英文版,则显示november | |
日 | dd | 当月第几天 | 21 |
ddd | 当年第几天 | 21 | |
dy | 当周第几天简写 | 星期五,若是英文版,则显示fri | |
day | 当周第几天全写 | 星期五,若是英文版,则显示friday | |
ddspth | 拼写 | twenty-first | |
时 | hh | 12小时进制 | 01 (范围:1:00:00 - 12:59:59) |
hh24 | 24小时进制 | 13 (范围:0:00:00 - 23:59:59) | |
分 | mi | 60进制 | 45 |
秒 | ss | 60进制 | 25 |
其它 | Q | 季度 | 4 |
WW | 当年第几周 | 44 | |
W | 当月第几周 | 1 |
例如:
select to_char(sysdate) "默认",
to_char(sysdate,'dd-mm-yyyy') "dd-mm-yyyy",
to_char(sysdate,'yyyy-mm-dd') "yyyy-mm-dd",
to_char(sysdate,'yy') "yy",
to_char(sysdate,'yyy') "yyy",
to_char(sysdate,'yyyy') "yyyy",
to_char(sysdate,'mm') "数字月",
to_char(sysdate,'mon') "简写月",
to_char(sysdate,'month') "全写月",
to_char(sysdate,'d') "当周第几天",
to_char(sysdate,'dd') "当月第几天",
to_char(sysdate,'ddd') "当年第几天",
to_char(sysdate,'dy') "当周第几天简写",
to_char(sysdate,'day') "当周第几天全写",
to_char(sysdate,'ddspth') "ddspth",
to_char(sysdate,'Q') "季度",
to_char(sysdate,'WW') "当年第几周",
to_char(sysdate,'W') "当月第几周",
to_char(sysdate,'hh24:mi:ss') "hh24:mi:ss",
to_char(sysdate,'yy-mm-dd') "yy-mm-dd",
to_char(sysdate,'yyyy-mm-dd') "yyyy-mm-dd",
to_char(sysdate,'yy-mm-dd hh12:mi:ss') "yy-mm-dd hh12:mi:ss",
to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') "yyyy-mm-dd hh12:mi:ss",
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') "yyyy-mm-dd hh24:mi:ss"
from dual;
● to_char()
日期和字符串之间的转换:
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;
select to_date('2008-11-21 13:45:25','yyyy-mm-dd hh24:mi:ss') from dual;
获得某天是星期几:
select to_char(to_date('2008-11-21 13:45:25','yyyy-mm-dd'),'day') from dual;
select to_char(to_date('2008-11-21 13:45:25','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE =American') from dual;
● round() 舍入到最接近的日期
select sysdate S1,
round(sysdate)S2,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH,
round(sysdate,'day') DAY
from dual;
● trunc() 截断到最接近的日期(单位为天)
select sysdate S1,
trunc(sysdate)S2, /*返回当前日期,无时分秒 */
trunc(sysdate,'year') YEAR, /*返回当前年的1月1日,无时分秒 */
trunc(sysdate,'month') MONTH, /*返回当前月的1日,无时分秒 */
trunc(sysdate,'day') DAY /*返回当前星期的星期天,无时分秒 */
from dual;
● next_day(datetime,day) 返回下个星期的日期,day为“1~7”或“星期日~星期六”,“1”表示星期日
select next_day(to_date('2008-11-21', 'yyyy-mm-dd'), 6) from dual;
● last_day()
最后一天:
select last_day(sysdate) from dual;
查找月的第一和最后一天:
select Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
Trunc(SYSDATE,'MONTH') - 1 / 86400 Last_Day_Last_Month,Trunc(SYSDATE,'MONTH') First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE,'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month from dual;
● add_months()
当前日期减到两个月:
select add_months(last_day(sysdate) + 1, -2) from dual;
获得今年天数:
select add_months(trunc(sysdate, 'year'), 12) - trunc(sysdate, 'year') from dual;
● new_time() 时区处理
select to_char(NEW_TIME(sysdate, 'GMT', 'EST'), 'dd/mm/yyyy hh:mi:ss'),sysdate from dual;
● greatest() 返回日期列表中最晚的日期
select greatest('01-1月-04', '04-1月-04', '10-2月-04') from dual;
● 拼写
select to_char( to_date(222,'J'),'Jsp') from dual;
显示:Two HundredTwenty-Two
● 日期运算。Oracle时间相加减以天为单位,设变量为n,换算成年、月、日
select floor(to_number(sysdate - to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss')) / 365) "相差年" from dual;
select floor(sysdate - to_date('20081121', 'yyyymmdd')) "相差天数" from dual;
select floor(sysdate - to_date('20081121', 'yyyy-mm-dd')) "相差天数" from dual;
select floor(sysdate - to_date('2008-11-21', 'yyyy-mm-dd')) "相差天数" from dual;
select floor(to_number(sysdate - to_date('2008-11-21', 'yyyy-mm-dd'))) "相差天数" from dual;
select months_between(to_date('2008-12-01', 'yyyy-mm-dd'),to_date('2008-10-01','yyyy-mm-dd')) "相差月" from dual;
select months_between(to_date('2008-2-1', 'yyyy-mm-dd'),to_date('2007-12-31','yyyy-mm-dd')) "相差月" from dual;
select floor(to_number(sysdate - to_date('2008-10-21 24:00:00','yyyy-mm-dd hh24:mi:ss')) * 24) "相差时" from dual;
select floor(to_number(sysdate - to_date('2008-10-21 24:00:00','yyyy-mm-dd hh24:mi:ss')) * 24 * 60) "相差分" from dual;
select floor(to_number(sysdate - to_date('2008-10-21 24:00:00','yyyy-mm-dd hh24:mi:ss')) * 24 * 60 * 60) "相差秒" from dual;
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate + n * 365, 'yyyy-mm-dd hh24:mi:ss') "改变时间-年" from dual;
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) "改变时间-月" from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate + n, 'yyyy-mm-dd hh24:mi:ss') "改变时间-日" from dual;
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate + n / 24, 'yyyy-mm-dd hh24:mi:ss') "改变时间-时" from dual;
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate + n / 24 / 60, 'yyyy-mm-dd hh24:mi:ss') "改变时间-分" from dual;
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate + n / 24 / 60 / 60, 'yyyy-mm-dd hh24:mi:ss') "改变时间-秒" from dual;
● 设置日期格式
日期格式要看你安装的ORACLE字符集的类型,如:US7ASCII,日期是'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;
可以用如下SQL语句查看参数:
select * from nls_session_parameters;
显示:
PARAMETER VALUE
NLS_LANGUAGESIMPLIFIED CHINESE
NLS_TERRITORY CHINA
NLS_CURRENCY RMB
NLS_ISO_CURRENCY CHINA
NLS_NUMERIC_CHARACTERS.,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE SIMPLIFIED CHINESE
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY RMB
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
而
select *from V$NLS_PARAMETERS;
显示:
PARAMETER VALUE
NLS_LANGUAGESIMPLIFIED CHINESE
NLS_TERRITORY CHINA
NLS_CURRENCY RMB
NLS_ISO_CURRENCY CHINA
NLS_NUMERIC_CHARACTERS.,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGESIMPLIFIED CHINESE
NLS_CHARACTERSET ZHS16GBK
NLS_SORT BINARY
NLS_TIME_FORMATHH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY RMB
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE