ORACLE中的單行函式 (2)

gaopengtttt發表於2012-04-24

4、轉換型函式

 

Round:四捨五入日期時間,可以是天,也可以是小時,分

EXP

select round(sysdate,'MI') from dual;

select round(sysdate,'HH') from dual;

select round(sysdate) from dual;

 

Trunc:截斷日期,可以是天,也可以是小時,分

Exp:

select trunc(sysdate,'MI') from dual;

 

cast:進行資料型別的轉化

Exp

select cast(sysdate as  varchar2(100)) from dual;

 

NUMTODSINTERVAL:將間隔指定為時分秒間隔型別

Exp

SELECT SYSDATE

      ,SYSDATE+NUMTODSINTERVAL(2,'HOUR') "2 hours later"

      ,SYSDATE+NUMTODSINTERVAL(30,'MINUTE') "30 minutes later"

      ,sysdate+1/12

      ,sysdate+1/48

FROM dual;

 

NUMTOYMINTERVAL:轉化間隔為年月型別

 

Exp

SELECT SYSDATE

      ,SYSDATE+NUMTOYMINTERVAL(2,'YEAR') "2 years later"

      ,SYSDATE+NUMTOYMINTERVAL(6,'MONTH') "6 months later"

FROM dual;

 

To_char:

Exp

SELECT TO_CHAR(SYSDATE, 'dd" day of "Month, YYYY" Quarter="q" weekday="day') from dual;

SELECT    to_char(sysdate,'" time="hh24-mi-ss.sssss" week of month="w" week of year="www') from dual;

      to_char(sysdate, '"day of a week="d" day of year="ddd') from dual;

SELECT SYSDATE

      ,TO_CHAR(SYSDATE,'Mmspth') Month

      ,TO_CHAR(SYSDATE,'DDth') Day

      ,TO_CHAR(SYSDATE,'Yyyysp') Year

      ,TO_CHAR(SYSDATE,'year') Year

,TO_CHAR(SYSDATE,'YEAR') Year

FROM dual;

 

SELECT TO_CHAR(123456,'9.99999EEEE')

      ,TO_CHAR(123456,'9.9EEEE')

FROM dual;

 

To_number: 轉化字元型別為數字型別

TO_DATE:字元型別轉換為日期型別

Exp

SQL> select      to_date('day of a week=3 day of year=115', '"day of a week="d" day of year="ddd') from dual;

 

TO_DATE('DAYOFAWEEK=3DAYOFYEAR

------------------------------

2012/4/24

 

TO_DSINTERVAL:轉換為日期可以接受的型別。

 

SELECT SYSDATE

      ,SYSDATE+('007 12:00:00') "+7 1/2  days"

      ,SYSDATE+7.5 "+7 1/2  days"

FROM dual;

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-722151/,如需轉載,請註明出處,否則將追究法律責任。

相關文章