📜  PL / SQL-日期和时间

📅  最后修改于: 2020-11-26 06:03:33             🧑  作者: Mango


在本章中,我们将讨论PL / SQL中的日期和时间。 PL / SQL中有两类与日期和时间相关的数据类型-

  • 日期时间数据类型
  • 间隔数据类型

日期时间数据类型是-

  • 日期
  • 时间戳
  • 时区时间戳
  • 与当地时区的时间戳

间隔数据类型为-

  • 每月间隔
  • 第二天的间隔时间

日期时间和间隔数据类型的字段值

datetimeinterval数据类型都由字段组成。这些字段的值确定数据类型的值。下表列出了日期时间和间隔的字段及其可能的值。

Field Name Valid Datetime Values Valid Interval Values
YEAR -4712 to 9999 (excluding year 0) Any nonzero integer
MONTH 01 to 12 0 to 11
DAY 01 to 31 (limited by the values of MONTH and YEAR, according to the rules of the calendar for the locale) Any nonzero integer
HOUR 00 to 23 0 to 23
MINUTE 00 to 59 0 to 59
SECOND

00 to 59.9(n), where 9(n) is the precision of time fractional seconds

The 9(n) portion is not applicable for DATE.

0 to 59.9(n), where 9(n) is the precision of interval fractional seconds
TIMEZONE_HOUR

-12 to 14 (range accommodates daylight savings time changes)

Not applicable for DATE or TIMESTAMP.

Not applicable
TIMEZONE_MINUTE

00 to 59

Not applicable for DATE or TIMESTAMP.

Not applicable
TIMEZONE_REGION Not applicable for DATE or TIMESTAMP. Not applicable
TIMEZONE_ABBR Not applicable for DATE or TIMESTAMP. Not applicable

日期时间数据类型和函数

以下是Datetime数据类型-

日期

它以字符和数字数据类型存储日期和时间信息。它由世纪,年,月,日,时,分和秒的信息组成。它指定为-

时间戳

它是DATE数据类型的扩展。它存储DATE数据类型的年,月和日,以及时,分和秒值。这对于存储精确的时间值很有用。

时区时间戳

它是TIMESTAMP的一种变体,在其值中包含时区区域名称或时区偏移。时区偏移量是当地时间与UTC之间的时差(以小时和分钟为单位)。此数据类型对于跨地理区域收集和评估日期信息很有用。

与当地时区的时间戳

它是TIMESTAMP的另一个变体,其值包含时区偏移。

下表提供了Datetime函数(其中,x具有datetime值)-

S.No Function Name & Description
1

ADD_MONTHS(x, y);

Adds y months to x.

2

LAST_DAY(x);

Returns the last day of the month.

3

MONTHS_BETWEEN(x, y);

Returns the number of months between x and y.

4

NEXT_DAY(x, day);

Returns the datetime of the next day after x.

5

NEW_TIME;

Returns the time/day value from a time zone specified by the user.

6

ROUND(x [, unit]);

Rounds x.

7

SYSDATE();

Returns the current datetime.

8

TRUNC(x [, unit]);

Truncates x.

时间戳函数(其中,x具有时间戳值)-

S.No Function Name & Description
1

CURRENT_TIMESTAMP();

Returns a TIMESTAMP WITH TIME ZONE containing the current session time along with the session time zone.

2

EXTRACT({ YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | { TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | } TIMEZONE_ABBR ) FROM x)

Extracts and returns a year, month, day, hour, minute, second, or time zone from x.

3

FROM_TZ(x, time_zone);

Converts the TIMESTAMP x and the time zone specified by time_zone to a TIMESTAMP WITH TIMEZONE.

4

LOCALTIMESTAMP();

Returns a TIMESTAMP containing the local time in the session time zone.

5

SYSTIMESTAMP();

Returns a TIMESTAMP WITH TIME ZONE containing the current database time along with the database time zone.

6

SYS_EXTRACT_UTC(x);

Converts the TIMESTAMP WITH TIMEZONE x to a TIMESTAMP containing the date and time in UTC.

7

TO_TIMESTAMP(x, [format]);

Converts the string x to a TIMESTAMP.

8

TO_TIMESTAMP_TZ(x, [format]);

Converts the string x to a TIMESTAMP WITH TIMEZONE.

例子

以下代码段说明了上述功能的用法-

例子1

SELECT SYSDATE FROM DUAL; 

输出

08/31/2012 5:25:34 PM 

例子2

SELECT TO_CHAR(CURRENT_DATE, 'DD-MM-YYYY HH:MI:SS') FROM DUAL; 

输出

31-08-2012 05:26:14

例子3

SELECT ADD_MONTHS(SYSDATE, 5) FROM DUAL;

输出

01/31/2013 5:26:31 PM 

例子4

SELECT LOCALTIMESTAMP FROM DUAL; 

输出

8/31/2012 5:26:55.347000 PM 

间隔数据类型和函数

以下是间隔数据类型-

  • IINTERVAL YEAR TO MONTH-使用YEAR和MONTH日期时间字段存储一段时间。

  • 第二天间隔-以天,小时,分钟和秒为单位存储一段时间。

间隔函数

S.No Function Name & Description
1

NUMTODSINTERVAL(x, interval_unit);

Converts the number x to an INTERVAL DAY TO SECOND.

2

NUMTOYMINTERVAL(x, interval_unit);

Converts the number x to an INTERVAL YEAR TO MONTH.

3

TO_DSINTERVAL(x);

Converts the string x to an INTERVAL DAY TO SECOND.

4

TO_YMINTERVAL(x);

Converts the string x to an INTERVAL YEAR TO MONTH.