在oracle中有很多关于日期的函数,如:
1、add_months()用于从一个日期值增加或减少一些月份
date_value:=add_months(date_value,number_of_months)
例:
SQL> select add_months(sysdate,12) "Next Year" from dual;
Next Year
----------
13-11月-04
SQL> select add_months(sysdate,112) "Last Year" from dual;
Last Year
----------
13-3月 -13
SQL>
select add_months(sysdate,-12) "Last Year" from dual
Last Year
----------
12-NOV-06
2、current_date()返回当前会放时区中的当前日期
date_value:=current_date
SQL> column sessiontimezone for a15
SQL> select sessiontimezone,current_date from dual;
SESSIONTIMEZONE CURRENT_DA
--------------- ----------
+08:00 13-11月-03
SQL> alter session set time_zone='-11:00'
2 /
会话已更改。
SQL> select sessiontimezone,current_timestamp from dual;
SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ------------------------------------
-11:00 12-11月-03 04.59.13.668000 下午 -11:
00
SQL>
3、current_timestamp()以timestamp with time zone数据类型返回当前会放时区中的当前日期
timestamp_with_time_zone_value:=current_timestamp([timestamp_precision])
SQL> column sessiontimezone for a15
SQL> column current_timestamp format a36
SQL> select sessiontimezone,current_timestamp from dual;
SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ------------------------------------
+08:00 13-11月-03 11.56.28.160000 上午 +08:
00
SQL> alter session set time_zone='-11:00'
2 /
会话已更改。
SQL> select sessiontimezone,current_timestamp from dual;
SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ------------------------------------
-11:00 12-11月-03 04.58.00.243000 下午 -11:
00
SQL>
4、dbtimezone()返回时区
varchar_value:=dbtimezone
SQL> select dbtimezone from dual;
DBTIME
------
-07:00
SQL>
5、extract()找出日期或间隔值的字段值
date_value:=extract(date_field from [datetime_value|interval_value])
SQL> select extract(month from sysdate) "This Month" from dual;
This Month
----------
11
SQL> select extract(year from add_months(sysdate,36)) "3 Years Out" from dual;
3 Years Out
-----------
2006
SQL>
6、last_day()返回包含了日期参数的月份的最后一天的日期
date_value:=last_day(date_value)
SQL> select last_day(date'2000-02-01') "Leap Yr?" from dual;
Leap Yr?
----------
29-2月 -00
SQL> select last_day(sysdate) "Last day of this month" from dual;
Last day o
----------
30-11月-03
SQL>
7、localtimestamp()返回会话中的日期和时间
timestamp_value:=localtimestamp
SQL> column localtimestamp format a28
SQL> select localtimestamp from dual;
LOCALTIMESTAMP
----------------------------
13-11月-03 12.09.15.433000
下午
SQL> select localtimestamp,current_timestamp from dual;
LOCALTIMESTAMP CURRENT_TIMESTAMP
---------------------------- ------------------------------------
13-11月-03 12.09.31.006000 13-11月-03 12.09.31.006000 下午 +08:
下午 00
SQL> alter session set time_zone='-11:00';
会话已更改。
SQL> select localtimestamp,to_char(sysdate,'DD-MM-YYYY HH:MI:SS AM') "SYSDATE" from dual;
LOCALTIMESTAMP SYSDATE
---------------------------- ------------------------
12-11月-03 05.11.31.259000 13-11-2003 12:11:31 下午
下午
SQL>
8、months_between()判断两个日期之间的月份数量
number_value:=months_between(date_value,date_value)
SQL> select months_between(sysdate,date'1971-05-18') from dual;
MONTHS_BETWEEN(SYSDATE,DATE'1971-05-18')
----------------------------------------
389.855143
SQL> select months_between(sysdate,date'2001-01-01') from dual;
MONTHS_BETWEEN(SYSDATE,DATE'2001-01-01')
----------------------------------------
34.4035409
SQL>
9、next_day()给定一个日期值,返回由第二个参数指出的日子第一次出现在的日期值(应返回相应日子的名称字符串)
oracle sql日期比较
oracle sql日期比较:
在今天之前:
select * from up_date where update < to_date('2007-09-07 00:00:00 ','yyyy-mm-dd hh24:mi:ss')
select * from up_date where update <= to_date('2007-09-07 00:00:00 ','yyyy-mm-dd hh24:mi:ss')
在今天只后:
select * from up_date where update > to_date('2007-09-07 00:00:00 ','yyyy-mm-dd hh24:mi:ss')
select * from up_date where update >= to_date('2007-09-07 00:00:00 ','yyyy-mm-dd hh24:mi:ss')
精确时间:
select * from up_date where update = to_date('2007-09-07 00:00:00 ','yyyy-mm-dd hh24:mi:ss')
在某段时间内:
select * from up_date where update between to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and update > to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from up_date where update <= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and update >= to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
SELECT count(*)
FROM LECTURE.T_TRACE_LICENSE Tbl
where bqid=’0009′
and tdate>=to_date(’2006-4-1
00:00
:00′, ‘yyyy-mm-dd hh24:mi:ss’)
and tdate<to_date(’2006-7-1
00:00
:00′, ‘yyyy-mm-dd hh24:mi:ss’)
注意的是把字符串转换为日期时需要指定日期类型.
程序中需要把to_date直接写在sql语句里.
tips :好像直接用也可以,不用to_date() :-)我一直这么用,但是如果要显示的分秒但是DB兵看不见,需要设置那样的format
================================================
trunc实际上是truncate函数,字面意思是截断,截尾。函数的功能是将数字进行截断。例如 tranc(1234.5678,2)的结果为1234.5600。tranc()并不四舍五入。再举例: tranc(1234.5678,0)的结果为1234.0000;tranc(1234.5678,-2)的结果为1200.0000。
函数形式:
trunc(date,[fmt])
意思:返回截取时间部分后的DATE,如果FMT部分有设置返回与该部分最近的日期
TRUNC(SYSDATE,'MM')+1
日期+数字(以日为单位),请记住!
============================================================
详细讲述 Oracle 中的 to_date 参数含义
日期格式参数 含义说明
D 一周中的星期几
DAY 天的名字,使用空格填充到9个字符
DD 月中的第几天
DDD 年中的第几天
2U
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. 日期到字符操作
select sysdate,to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) from dual
>> 12-NOV-07 2007-11-12 10:59:26
select sysdate,to_char(sysdate,’yyyy-mm-dd hh:mi:ss’) from dual
>> 12-NOV-07 2007-11-12 10:59:44
select sysdate,to_char(sysdate,’yyyy-ddd hh:mi:ss’) from dual
>> 12-NOV-07 2007-316 11:00:03
select sysdate,to_char(sysdate,’yyyy-mm iw-d hh:mi:ss’) from dual
>> 12-NOV-07 2007-11 46-2 11:00:17
参考oracle的相关关文档(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515)
3. 字符到日期操作
select to_date(’2003-10-17 21:15:37’,’yyyy-mm-dd hh24:mi:ss’) from dual
7
>> 17-OCT-03
具体用法和上面的to_char差不多。
4. trunk/ ROUND函数的使用
HW;py
select trunc(sysdate ,’YEAR’) from dual
>> 01-JAN-07
select trunc(sysdate ) from dual
>> 12-NOV-07
select to_char(trunc(sysdate ,’YYYY’),’YYYY’) from dual
>> 2007
5.oracle有毫秒级的数据类型
--返回当前时间 年月日小时分秒毫秒
select current_timestamp(5) from dual;
>> 12-NOV-07 11.07.40.569330000 AM GMT+ 08:00
select current_timestamp from dual;
>> 12-NOV-07 11.07.21.569765000 AM GMT+08:00]")z+~@
select to_char(current_timestamp(5),’DD-MON-YYYY HH24:MI:SSxFF’) from dual;
>> 12-NOV-2007 11:08:14.45481
--返回当前 时间的秒毫秒,可以指定秒后面的精度(最大=9)
3de‑FO
select to_char(current_timestamp(9),’MI:SSxFF’) from dual;
6. 计算程序运行的时间 (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;
本文介绍了Oracle数据库中的日期处理函数,包括日期增加、当前日期获取、日期字段抽取等实用功能,并提供了具体的SQL示例。
&spm=1001.2101.3001.5002&articleId=2150551&d=1&t=3&u=bee6549ba21a4e77afb6e6002d681e6b)
666

被折叠的 条评论
为什么被折叠?



