设为首页 加入收藏

TOP

在PL/SQL中使用日期类型(三)
2015-11-21 02:03:39 来源: 作者: 【 】 浏览:1
Tags:PL/SQL 使用 日期 类型
月份第一天 Get the first day of the month for the specified date:

l_date := TRUNC (SYSDATE, ‘MM’);

Get the first day of the quarter for the specified date:

l_date := TRUNC (SYSDATE, ‘Q’);

获取当前日期所在年份的第一天 Get the first day of the year for the specified date:

l_date := TRUNC (SYSDATE, ‘Y’);

SCOTT@orcl> select TRUNC (SYSDATE, 'Y') from dual; TRUNC(SYSDATE,'Y') ------------------- 2015-01-01 12:00:00

6、日期运算 Date arithmetic

针对日期和时间戳的运算,Oracle数据库提供了以下方式:

Add a numeric value to or subtract it from a date, as in SYSDATE + 7; Oracle Database treats the number as the number of days.
给指定日期加上或减去一个数值,例如:SYSDATE + 7; Oracle认为该数字单位为:天。

Add one date to or subtract it from another, as in l_hiredate - SYSDATE.
两个日期直接相加减,例如:l_hiredate - SYSDATE。

Use a built-in function to “move” a date by a specified number of months or to another date in a week.
使用内建函数使日期移动指定月数或到达周内的另外一个日期。

例如:
Set a local variable to tomorrow’s date: 设置日期变量为明天

l_date := SYSDATE + 1;

Move back one hour: 向前推1个小时
l_date := SYSDATE - 1/24;

Move ahead 10 seconds: 向前推10秒
l_date := SYSDATE + 10 / (60 * 60 * 24);

When you add one date to or subtract it from another, the result is the number of days between the two. As a result, executing this block:
如果你对2个日期相加减,结果是2个日期间隔的天数。

DECLARE l_date1 DATE := SYSDATE; l_date2 DATE := SYSDATE + 10; BEGIN DBMS_OUTPUT.put_line ( l_date2 - l_date1); DBMS_OUTPUT.put_line ( l_date1 - l_date2); END; returns the following output: 10 -10

And the following function can be used to compute the age of a person, assuming that the person’s correct birth date is passed as the function’s only argument:
另外以下函数可用于计算一个人的年龄:

CREATE OR REPLACE FUNCTION your_age (birthdate_in IN DATE) RETURN NUMBER IS BEGIN RETURN SYSDATE - birthdate_in; END your_age;

下面来看下几个内建函数:
ADD_MONTHS—针对日期或时间戳加减指定的月数

NEXT_DAY— 当前系统时间的下一星期?的时间 指定时间的下一个星期几(由char指定)所在的日期
NEXT_DAY(date,char)
date参数为日期型,
char:为1~7或Monday~Sunday

指定时间的下一个星期几(由char指定)所在的日期,
char也可用1~7替代,1表示星期日,2代表星期一。。。。
还可以是星期一、星期二。。。星期日

LAST_DAY—返回指定日期所在月份最后一天的日期

Move ahead one month: 往后推1个月【译者注:one month later】

l_date := ADD_MONTHS (SYSDATE, 1);

Move backward three months: 往前推3个月【译者注:one month ago】

l_date := ADD_MONTHS (SYSDATE, -3);

SCOTT@orcl> SELECT SYSDATE, 2 LAST_DAY(SYSDATE) "Last", 3 LAST_DAY(SYSDATE) - SYSDATE "Days Left" 4 FROM DUAL; SYSDATE Last Days Left ------------------- ------------------- ---------- 2015-07-25 07:04:17 2015-07-31 07:04:17 6

Code Listing 3: Calls to ADD_MONTHS

BEGIN DBMS_OUTPUT.put_line ( ADD_MONTHS (TO_DATE ('31-jan-2011', 'DD-MON-YYYY'), 1)); DBMS_OUTPUT.put_line ( ADD_MONTHS (TO_DATE ('27-feb-2011', 'DD-MON-YYYY'), -1)); DBMS_OUTPUT.put_line ( ADD_MONTHS (TO_DATE ('28-feb-2011', 'DD-MON-YYYY'), -1)); END; Here is the output: 28-FEB-11 27-JAN-11 31-JAN-11 

Find the next Saturday after today’s date:
找到今天过后的下一个星期六!

l_date := NEXT_DAY (SYSDATE, ‘SAT’);
– or
l_date := NEXT_DAY (SYSDATE, ‘SATURDAY’);

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇固定分组计算的sql简化 下一篇14-SQLite之主键与唯一约束

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: