Oracle date

 参考网址:http://www.oradev.com/oracle_date_format.jsp

1、Oracle支持五种格式的分隔符:

  -/,.;:

1 SELECT TO_CHAR(SYSDATE, 'yyyy-mm-dd') AS A,
2        TO_CHAR(SYSDATE, 'yyyy/mm/dd') AS B,
3        TO_CHAR(SYSDATE, 'YYYY,mm,dd') AS C,
4        TO_CHAR(SYSDATE, 'yyyy:mm:dd') AS D,
5        TO_CHAR(SYSDATE, 'yyyy;mm;dd')
6   FROM DUAL;

2、常用的年的格式有以下几种:

YYYY: All four digits of the year

Y,YYY: All four digits of the year with a comma

YYY: Last three digits of the year

YY: Last two digits of the year

Y: Last digit of the year

3、常用的周格式:

WW: Two-digit week of the year:一年的第几个周

W: One-digit week of the month:本月的第几个周

4、常用的日格式:

DDD: Three-digit day of the year--今天是一年的第XXX天

DD:Two-digit day of the month   --今天是一个月中的第XXX天

D: One-digit day of the week    --今天是一个周中的第XXX天]

DAY/Day/DY/dy: 今天是周XXX,结果是中文的具体星期数

5、常用的小时格式

HH24: Two-digit hour in 24-hour format(24小时制)

HH: Two-digit hour in 12-hour format(12小时制)

6、常用的分钟和秒数格式

MI: Two-digit minute

SS: Two-digit second

7、常见的集中日期格式转换

1 SELECT TO_CHAR(SYSDATE, 'yyyy/mm/dd') AS A,
2        TO_CHAR(SYSDATE, 'yyyy-mm-dd') AS B,
3        TO_CHAR(SYSDATE, 'dd/MM/YYYY') AS C,
4        TO_CHAR(SYSDATE, 'yyyy') || '' || TO_CHAR(SYSDATE, 'mm') || '' ||
5        TO_CHAR(SYSDATE, 'dd') || '' AS D
6   FROM DUAL;

--假如查询的月是小于10月的则会出现:01月,02月,可以使用如下函数处理

1 SELECT DECODE(SUBSTR((TO_CHAR(SYSDATE, 'mm')), 1, 1), 
2               '0',
3               SUBSTR((TO_CHAR(SYSDATE, 'mm')), 2, 1) || '')
4   FROM DUAL

EXTRACT()函数的使用:

 EXTRACT extracts and returns the value of a specified datetime field from a datetime or interval value expression.Some combinations of datetime field and datetime or interval value expression result in ambiguity. In these cases, Oracle returns 'UNKNOWN'(当格式错误的时候,返回的UNKOWN)

 使用TO_DATE或者是在日期前面加上DATE关键字都可以进行日期格式的转换。

例如:

SELECT * FROM EMP E WHERE E.HIREDATE < DATE '2012-08-09'

---

1 SELECT EXTRACT(YEAR FROM TO_DATE('1998-03-07', 'YYYY-MM-DD')) AS A,
2        EXTRACT(YEAR FROM DATE '1998-03-07') AS B
3   FROM DUAL;

 

Oracle date format

With the functions to_char and to_date, a date format can be used. Example:
select to_char(sysdate,'DD/MM/YYYY HH24:MI:SS') from dual;
will return something like: 24/03/2006 14:36:43

Here is a list of all the formats that can be used:

 

Format maskDescription
CCCentury
SCCCentury BC prefixed with -
YYYYYear with 4 numbers
SYYYYear BC prefixed with -
IYYYISO Year with 4 numbers
YYYear with 2 numbers
RRYear with 2 numbers with Y2k compatibility
YEARYear in characters
SYEARYear in characters, BC prefixed with -
BCBC/AD Indicator *
QQuarter in numbers (1,2,3,4)
MMMonth of year 01, 02...12
MONTHMonth in characters (i.e. January)
MONJAN, FEB
WWWeeknumber (i.e. 1)
WWeeknumber of the month (i.e. 5)
IWWeeknumber of the year in ISO standard.
DDDDay of year in numbers (i.e. 365)
DDDay of the month in numbers (i.e. 28)
DDay of week in numbers(i.e. 7)
DAYDay of the week in characters (i.e. Monday)
FMDAYDay of the week in characters (i.e. Monday)
DYDay of the week in short character description (i.e. SUN)
JJulian Day (number of days since January 1 4713 BC, where January 1 4713 BC is 1 in Oracle)
HHHournumber of the day (1-12)
HH12Hournumber of the day (1-12)
HH24Hournumber of the day with 24Hours notation (0-23)
AMAM or PM
PMAM or PM
MINumber of minutes (i.e. 59)
SSNumber of seconds (i.e. 59)
SSSSSNumber of seconds this day.
DSShort date format. Depends on NLS-settings. Use only with timestamp.
DLLong date format. Depends on NLS-settings. Use only with timestamp.
EAbbreviated era name. Valid only for calendars: Japanese Imperial, ROC Official and Thai Buddha.. (Input-only)
EEThe full era name
FFThe fractional seconds. Use with timestamp.
FF1..FF9The fractional seconds. Use with timestamp. The digit controls the number of decimal digits used for fractional seconds.
FMFill Mode: suppresses blianks in output from conversion
FXFormat Exact: requires exact pattern matching between data and format model.
IYY or IY or Ithe last 3,2,1 digits of the ISO standard year. Output only
RMThe Roman numeral representation of the month (I .. XII)
RRThe last 2 digits of the year.
RRRRThe last 2 digits of the year when used for output. Accepts fout-digit years when used for input.
SCCCentury. BC dates are prefixed with a minus.
CCCentury
SPSpelled format. Can appear of the end of a number element. The result is always in english. For example month 10 in format MMSP returns "ten"
SPTHSpelled and ordinal format; 1 results in first.
THConverts a number to it's ordinal format. For example 1 becoms 1st.
TSShort time format. Depends on NLS-settings. Use only with timestamp.
TZDAbbreviated time zone name. ie PST.
TZHTime zone hour displacement.
TZMTime zone minute displacement.
TZRTime zone region
XLocal radix character. In america this is a period (.)

转载于:https://www.cnblogs.com/caroline/archive/2012/04/22/2464595.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值