Oracle中的时间函数用法(to_date、to_char)

24小时的形式显示出来要用HH24<o:p></o:p>

select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;<o:p></o:p>

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

<o:p> </o:p>

to_date() function<o:p></o:p>

1.日期格式参数 含义说明  <o:p></o:p>

D 一周中的星期几  <o:p></o:p>

DAY 天的名字,使用空格填充到9个字符  <o:p></o:p>

DD 月中的第几天  <o:p></o:p>

DDD 年中的第几天  <o:p></o:p>

DY 天的简写名  <o:p></o:p>

IW ISO标准的年中的第几周  <o:p></o:p>

IYYY ISO标准的四位年份  <o:p></o:p>

YYYY 四位年份  <o:p></o:p>

YYY,YY,Y 年份的最后三位,两位,一位  <o:p></o:p>

HH 小时,按12小时计  <o:p></o:p>

HH24 小时,按24小时计  <o:p></o:p>

MI   <o:p></o:p>

SS   <o:p></o:p>

MM   <o:p></o:p>

Mon 月份的简写  <o:p></o:p>

Month 月份的全名  <o:p></o:p>

W 该月的第几个星期  <o:p></o:p>

WW 年中的第几个星期     1.日期时间间隔操作 <o:p></o:p>

当前时间减去7分钟的时间 <o:p></o:p>

select sysdate,sysdate - interval '7' MINUTE from dual <o:p></o:p>

当前时间减去7小时的时间 <o:p></o:p>

select sysdate - interval '7' hour from dual <o:p></o:p>

当前时间减去7天的时间 <o:p></o:p>

select sysdate - interval '7' day from dual <o:p></o:p>

当前时间减去7月的时间 <o:p></o:p>

select sysdate,sysdate - interval '7' month from dual <o:p></o:p>

当前时间减去7年的时间 <o:p></o:p>

select sysdate,sysdate - interval '7' year from dual <o:p></o:p>

时间间隔乘以一个数字 <o:p></o:p>

select sysdate,sysdate - 8 *interval '2' hour from dual <o:p></o:p>

<o:p> </o:p>

2.日期到字符操作 <o:p></o:p>

select sysdate,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual <o:p></o:p>

select sysdate,to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual <o:p></o:p>

select sysdate,to_char(sysdate,'yyyy-ddd hh:mi:ss') from dual <o:p></o:p>

select sysdate,to_char(sysdate,'yyyy-mm iw-d hh:mi:ss') from dual <o:p></o:p>

参考oracle的相关关文档(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515) <o:p></o:p>

<o:p> </o:p>

3. 字符到日期操作 <o:p></o:p>

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

具体用法和上面的to_char差不多。 <o:p></o:p>

<o:p> </o:p>

4. trunk/ ROUND函数的使用 <o:p></o:p>

select trunc(sysdate ,'YEAR') from dual <o:p></o:p>

select trunc(sysdate ) from dual <o:p></o:p>

select to_char(trunc(sysdate ,'YYYY'),'YYYY') from dual <o:p></o:p>

<o:p> </o:p>

5.oracle有毫秒级的数据类型 <o:p></o:p>

--返回当前时间 年月日小时分秒毫秒 <o:p></o:p>

select to_char(current_timestamp(5),'DD-MON-YYYY HH24:MI:SSxFF') from dual; <o:p></o:p>

--返回当前 时间的秒毫秒,可以指定秒后面的精度(最大=9) <o:p></o:p>

select to_char(current_timestamp(9),'MI:SSxFF') from dual; <o:p></o:p>

<o:p> </o:p>

6.计算程序运行的时间(ms) <o:p></o:p>

declare <o:p></o:p>

type rc is ref cursor; <o:p></o:p>

l_rc rc; <o:p></o:p>

l_dummy all_objects.object_name%type; <o:p></o:p>

l_start number default dbms_utility.get_time; <o:p></o:p>

begin <o:p></o:p>

for I in 1 .. 1000 <o:p></o:p>

loop <o:p></o:p>

open l_rc for <o:p></o:p>

'select object_name from all_objects '|| <o:p></o:p>

'where object_id = ' || i; <o:p></o:p>

fetch l_rc into l_dummy; <o:p></o:p>

close l_rc; <o:p></o:p>

end loop; <o:p></o:p>

dbms_output.put_line <o:p></o:p>

( round( (dbms_utility.get_time-l_start)/100, 2 ) || <o:p></o:p>

' seconds...' ); <o:p></o:p>

end;<o:p></o:p>

<o:p> </o:p>

to_char() function<o:p></o:p>

The following are number examples for the to_char function.<o:p></o:p>

to_char(1210.73, '9999.9')<o:p></o:p>

would return '1210.7'<o:p></o:p>

to_char(1210.73, '9,999.99')<o:p></o:p>

would return '1,210.73'<o:p></o:p>

to_char(1210.73, '$9,999.00')<o:p></o:p>

would return '$1,210.73'<o:p></o:p>

to_char(21, '000099')<o:p></o:p>

would return '000021'<o:p></o:p>

 <o:p></o:p>

The following is a list of valid parameters when the to_char function is used to convert a date to a string. These parameters can be used in many combinations.<o:p></o:p>

  <o:p></o:p>

Parameter<o:p></o:p>

Explanation<o:p></o:p>

YEAR<o:p></o:p>

Year, spelled out<o:p></o:p>

YYYY<o:p></o:p>

4-digit year<o:p></o:p>

YYY
YY
Y<o:p></o:p>

Last 3, 2, or 1 digit(s) of year.<o:p></o:p>

IYY
IY
I<o:p></o:p>

Last 3, 2, or 1 digit(s) of ISO year.<o:p></o:p>

IYYY<o:p></o:p>

4-digit year based on the ISO standard<o:p></o:p>

Q<o:p></o:p>

Quarter of year (1, 2, 3, 4; JAN-MAR = 1).<o:p></o:p>

MM<o:p></o:p>

Month (01-12; JAN = 01).<o:p></o:p>

MON<o:p></o:p>

Abbreviated name of month.<o:p></o:p>

MONTH<o:p></o:p>

Name of month, padded with blanks to length of 9 characters.<o:p></o:p>

RM<o:p></o:p>

Roman numeral month (I-XII; JAN = I).<o:p></o:p>

WW<o:p></o:p>

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.<o:p></o:p>

W<o:p></o:p>

Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.<o:p></o:p>

IW<o:p></o:p>

Week of year (1-52 or 1-53) based on the ISO standard.<o:p></o:p>

D<o:p></o:p>

Day of week (1-7).<o:p></o:p>

DAY<o:p></o:p>

Name of day.<o:p></o:p>

DD<o:p></o:p>

Day of month (1-31).<o:p></o:p>

DDD<o:p></o:p>

Day of year (1-366).<o:p></o:p>

DY<o:p></o:p>

Abbreviated name of day.<o:p></o:p>

J<o:p></o:p>

Julian day; the number of days since January 1, 4712 BC.<o:p></o:p>

HH<o:p></o:p>

Hour of day (1-12).<o:p></o:p>

HH12<o:p></o:p>

Hour of day (1-12).<o:p></o:p>

HH24<o:p></o:p>

Hour of day (0-23).<o:p></o:p>

MI<o:p></o:p>

Minute (0-59).<o:p></o:p>

SS<o:p></o:p>

Second (0-59).<o:p></o:p>

SSSSS<o:p></o:p>

Seconds past midnight (0-86399).<o:p></o:p>

FF<o:p></o:p>

Fractional seconds.<o:p></o:p>


<o:p></o:p>

The following are date examples for the to_char function.<o:p></o:p>

to_char(sysdate, 'yyyy/mm/dd');<o:p></o:p>

would return '<st1:chsdate isrocdate="False" month="7" day="9" islunardate="False" w:st="on" year="2003">2003/07/09</st1:chsdate>'<o:p></o:p>

to_char(sysdate, 'Month DD, YYYY');<o:p></o:p>

would return 'July 09, 2003'<o:p></o:p>

to_char(sysdate, 'FMMonth DD, YYYY');<o:p></o:p>

would return 'July 9, 2003'<o:p></o:p>

to_char(sysdate, 'MON DDth, YYYY');<o:p></o:p>

would return 'JUL 09TH, 2003'<o:p></o:p>

to_char(sysdate, 'FMMON DDth, YYYY');<o:p></o:p>

would return 'JUL 9TH, 2003'<o:p></o:p>

to_char(sysdate, 'FMMon ddth, YYYY');<o:p></o:p>

would return 'Jul 9th, 2003'<o:p></o:p>


<o:p></o:p>

You will notice that in some examples, the format_mask parameter begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the examples below.<o:p></o:p>

to_char(sysdate, 'FMMonth DD, YYYY');<o:p></o:p>

would return 'July 9, 2003'<o:p></o:p>

to_char(sysdate, 'FMMON DDth, YYYY');<o:p></o:p>

would return 'JUL 9TH, 2003'<o:p></o:p>

to_char(sysdate, 'FMMon ddth, YYYY');<o:p></o:p>

would return 'Jul 9th, 2003'<o:p></o:p>

The zeros have been suppressed so that the day component shows as "9" as opposed to "09".<o:p></o:p>


<o:p></o:p>

你可能感兴趣的:(oracle,sql,SQL Server,J#)