Oracle中的時間函數用法(to_date、to_char) (總結)


一、24小時的形式顯示出來要用HH24

select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;

select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;

二、to_date() function

1.日期格式參數含義說明 

D一周中的星期幾 

DAY天的名字,使用空格填充到9個字符 

DD月中的第幾天 

DDD年中的第幾天 

DY天的簡寫名 

IW ISO標准的年中的第幾周 

IYYY ISO標准的四位年份 

YYYY四位年份 

YYY,YY,Y年份的最后三位,兩位,一位 

HH小時,按12小時計 

HH24小時,按24小時計 

MI分 

SS秒 

MM月 

Mon月份的簡寫 

Month月份的全名 

W該月的第幾個星期 

WW年中的第幾個星期     1.日期時間間隔操作

當前時間減去7分鍾的時間

select sysdate,sysdate - interval '7' MINUTE from dual

當前時間減去7小時的時間

select sysdate - interval '7' hour from dual

當前時間減去7天的時間

select sysdate - interval '7' day from dual

當前時間減去7月的時間

select sysdate,sysdate - interval '7' month from dual

當前時間減去7年的時間

select sysdate,sysdate - interval '7' year from dual

時間間隔乘以一個數字

select sysdate,sysdate - 8 *interval '2' hour from dual

2.1.日期到字符操作

select sysdate,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual

select sysdate,to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual

select sysdate,to_char(sysdate,'yyyy-ddd hh:mi:ss') from dual

select sysdate,to_char(sysdate,'yyyy-mm iw-d hh:mi:ss') from dual

2.2.字符到日期操作

select to_date('2003-10-17 21:15:37','yyyy-mm-dd hh24:mi:ss') from dual

2.3.oracle有毫秒級的數據類型

--返回當前時間年月日小時分秒毫秒

select to_char(current_timestamp(5),'DD-MON-YYYY HH24:MI:SSxFF') from dual;

--返回當前時間的秒毫秒,可以指定秒后面的精度(最大=9)

select to_char(current_timestamp(9),'MI:SSxFF') from dual;

2.4.計算程序運行的時間(ms)

declare

type rc is ref cursor;

l_rc rc;

l_dummy all_objects.object_name%type;

l_start number default dbms_utility.get_time;

begin

for I in 1 .. 1000

loop

open l_rc for

'select object_name from all_objects '||

'where object_id = ' || i;

fetch l_rc into l_dummy;

close l_rc;

end loop;

dbms_output.put_line

( round( (dbms_utility.get_time-l_start)/100, 2 ) ||

' seconds...' );

end;

三、to_char() function

The following are number examples for theto_charfunction.

 

 

to_char(1210.73, '9999.9')

would return '1210.7'

to_char(1210.73, '9,999.99')

would return '1,210.73'

to_char(1210.73, '$9,999.00')

would return '$1,210.73'

to_char(21, '000099')

would return '000021'

 

The following is a list of valid parameters when theto_charfunction is used to convert a date to a string. These parameters can be used in many combinations.

 

 

Parameter

Explanation

YEAR

Year, spelled out

YYYY

4-digit year

YYY
YY
Y

Last 3, 2, or 1 digit(s) of year.

IYY
IY
I

Last 3, 2, or 1 digit(s) of ISO year.

IYYY

4-digit year based on the ISO standard

Q

Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM

Month (01-12; JAN = 01).

MON

Abbreviated name of month.

MONTH

Name of month, padded with blanks to length of 9 characters.

RM

Roman numeral month (I-XII; JAN = I).

WW

Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.

W

Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.

IW

Week of year (1-52 or 1-53) based on the ISO standard.

D

Day of week (1-7).

DAY

Name of day.

DD

Day of month (1-31).

DDD

Day of year (1-366).

DY

Abbreviated name of day.

J

Julian day; the number of days since January 1, 4712 BC.

HH

Hour of day (1-12).

HH12

Hour of day (1-12).

HH24

Hour of day (0-23).

MI

Minute (0-59).

SS

Second (0-59).

SSSSS

Seconds past midnight (0-86399).

FF

Fractional seconds.

 

 

The following are date examples for theto_charfunction.

 

 

to_char(sysdate, 'yyyy/mm/dd');

would return '2003/07/09'

to_char(sysdate, 'Month DD, YYYY');

would return 'July 09, 2003'

to_char(sysdate, 'FMMonth DD, YYYY');

would return 'July 9, 2003'

to_char(sysdate, 'MON DDth, YYYY');

would return 'JUL 09TH, 2003'

to_char(sysdate, 'FMMON DDth, YYYY');

would return 'JUL 9TH, 2003'

to_char(sysdate, 'FMMon ddth, YYYY');

would return 'Jul 9th, 2003'

 

 

You will notice that in some examples, theformat_maskparameter begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the examples below.

 

 

to_char(sysdate, 'FMMonth DD, YYYY');

would return 'July 9, 2003'

to_char(sysdate, 'FMMON DDth, YYYY');

would return 'JUL 9TH, 2003'

to_char(sysdate, 'FMMon ddth, YYYY');

would return 'Jul 9th, 2003'

 

The zeros have been suppressed so that the day component shows as "9" as opposed to "09".

四、Oracle函數to_char轉化數字型指定小數點位數的用法

to_char,函數功能,就是將數值型或者日期型轉化為字符型。

比如最簡單的應用:

/*1.0123--->'1.0123'*/
Select TO_CHAR(1.0123) FROM DUAL
/*123--->'123'*/
Select TO_CHAR(123) FROM DUAL

 接下來再看看下面:

/*0.123 ---> '.123' */
SELEC TO_CHAR(0.123) FROM DUAL

 上面的結果'.123'在大多數情況下都不是我們想要的結果,我們想要的應該是'0.123'。

我們來看一下to_char函數的具體用法:

TO_CHAR ( n [, fmt [, 'nlsparam']] )

該函數將NUMBER類型的n按數值格式fmt轉換成VARCHAR2類型的值。'nlsparams'指定由數值格式的元素返回的字符,包括:

.小數點字符

.組分隔符

.本地錢幣符號

.國際錢幣符號

變元的形式為:

'NLS_NUMERIC_CHARACTERS="dg" NLS_CURRENCY="tcxt" NLS_ISO_CURRENCY=territory'

其中d為小數點字符,g為組分隔符。

例 :TO_CHAR (17145,'L099G999','NLS_NUMERIC_CHARACTERS=".," NLS_CURRENCY="NUD"')=NUD017,145

通過上面的了解,再查看fmt的一些格式,我們可以用以下表達式得到'0.123'的值:

/*0.123 ---> ' 0.123' */ 
Select TO_CHAR(0.123,'0.999') FROM DUAL
/*100.12 ---> '######' */ 
Select TO_CHAR(100.12,'0.999') FROM DUAL
/*1.12 ---> ' 1.120' */ 
Select TO_CHAR(1.12,'0.999') FROM DUAL

 ' 0.123'是出來了,可是前面又多了一個空格。

對於100.12的值卻是######,以及'1.12'的值變成了'1.120'。

我們重新確定一個新的需求:

1、去空格

2、小數點最多4位,最少保留2位。

    1--->'1.00';1.1--->'1.00';1.12-->'1.12';1.1234--->'1.1234';

    1.12345--->'1.1235'

最終實現如下:

/*
  FM :除空格  
  9999999.0099:允許小數點左邊最大正數為7位,小數點右邊最少2位,最多4位,且在第5位進行四舍五入
*/ 
Select TO_CHAR(123.0233,'FM9999999.0099') FROM DUAL

五、PLSQL小經驗http://hi.baidu.com/seekbug/blog/item/f523934513b9dd3d87947337.html

一、Oracle的to_char()函數功能很強大但是在用它格式化數值型數據時應該請注意以下幾項。如果是小數如:0.23這樣的數據經過to_char后再顯示會變為.23,如果你為了讓它顯示出原來的0.23的話就得用To_char(要格式化的數值,’0.999’)保留三個小數,並顯示但這里就要注意了。他為你截取小數的時候是四舍五入了。所以如果是要求截掉小數而不四舍五入的話就應該自己寫個函數截下去后再規格化。以保證它不四舍五入。

二、To_char(1.9999,’FM90.0999’)這個函數規格化時90.0999的含義是有9的地方如果有數字就顯示如果沒有數字就不顯示,有0的地方在沒有數字的時候也會有0來占位.但這樣做也有一個很大的缺點,就是如果是整數時它也仍然會顯示”.”,不要小瞧這個點,一般來講頁面上要顯示的話這個點就是多余的.也給我們造成了不小的麻煩.還要自己再寫函數來把這個小點搞定.

三、對於日期型的Oracle倒時提供了一個好的處理方法,可以把日期做成數值型的.然后再To_char就能顯示出你所需要的類型了
.

四、在使用select into時一定要注意,這種方法你一定要確認肯定會有數據被查出時才能使用.如果查詢結果為空時會導致報錯.還有一種情況是查出來的數據是多條也會報錯.所以應該盡量便宜游標來做.會減少錯誤產生的機率
.

五、還有注意一點rownum不支持排序,就是說你想用這個來控制行數的話就會發現他沒有按你指定的排序方式顯示.,這是一個很難辦的事.而且如果你用rownum=2這樣的語句來輸出第二行的話也是行不通的.六、最惡心的一點是Oracle對null的判斷變態到極點.如果你說某個變量aa=null它是判斷不出來的.盡管aa的確是空.即使在選擇條件里也是判斷不出來的.不知道為什么,只好用nvl()這個函數來判斷了.在條件之外可以用aa is null來判斷.

六、補充一點.就是在寫存儲過程時要注意參數名不能與數據庫字段名相同.否則Oracle會把這個參數名看成是字段名的,即使你用表的別名區分也不行.所以起參數名的時候一定要注意這點了。

 

引自:http://space.itpub.net/8334342/viewspace-434669


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM