Working with Dates in PL/SQL

By Steven Feuerstein  史蒂芬.佛伊尔斯坦

The previous articles in this introductory PL/SQL series focused on working with strings and numbers in PL/SQL-based applications. Without a doubt, strings and numbers are important, but it is certainly a very rare application that does not also rely on dates. You need to keep track of when events occurred, when people were born, and much more.

As a result, you will quite often need to

  • Declare variables and constants for dates

  • Use built-in functions to display and modify date values

  • Perform computations on dates

A date is also a considerably more complex datatype than a string or a number. It has multiple parts (year, month, day, hour, and so on), and there are many rules about what constitutes a valid date. This article gives you all the information you need in order to begin working with dates in your PL/SQL programs.

Dates, Time Stamps, and Intervals in PL/SQL

Most applications require the storage and manipulation of dates and times. Unlike strings and numbers, dates are quite complicated: not only are they highly formatted data, but there are also many rules for determining valid values and valid calculations (leap days and years, daylight saving time changes, national and company holidays, date ranges, and so on).

Fortunately, Oracle Database and PL/SQL provide a set of true date and time datatypes that store both date and time information in a standard internal format, and they also have an extensive set of built-in functions for manipulating the date and time.

There are three datatypes you can use to work with dates and times:

  • DATE—This datatype stores a date and a time, resolved to the second. It does not include the time zone. DATE is the oldest and most commonly used datatype for working with dates in Oracle applications.

  • TIMESTAMP—Time stamps are similar to dates, but with these two key distinctions: (1) you can store and manipulate times resolved to the nearest billionth of a second (9 decimal places of precision), and (2) you can associate a time zone with a time stamp, and Oracle Database will take that time zone into account when manipulating the time stamp.

  • INTERVAL—Whereas DATE and TIMESTAMP record a specific point in time, INTERVAL records and computes a time duration. You can specify an interval in terms of years and months, or days and seconds.

Listing 1 includes example variables whose declaration is based on these datatypes.

Code Listing 1: Declaring DATE, TIMESTAMP, and INTERVAL variables

DECLARE
l_today_date DATE := SYSDATE;
l_today_timestamp TIMESTAMP := SYSTIMESTAMP;
l_today_timetzone TIMESTAMP WITH TIME ZONE := SYSTIMESTAMP;
l_interval1 INTERVAL YEAR (4) TO MONTH := '2011-11';
l_interval2 INTERVAL DAY (2) TO SECOND := '15 00:30:44';
BEGIN
null;
END;

Working with intervals and time stamps with time zones can be very complicated; relatively few developers will need these more advanced features. This article focuses on the core DATE and TIMESTAMP types, along with the most commonly used built-in functions.

Choosing a datatype. With such an abundance of riches, how do you decide which of these date-and-time datatypes to use? Here are some guidelines:

  • Use one of the TIMESTAMP types if you need to track time down to a fraction of a second.

  • You can, in general, use TIMESTAMP in place of DATE. A time stamp that does not contain subsecond precision takes up 7 bytes of storage, just as a DATE datatype does. When your time stamp does contain subsecond data, it takes up 11 bytes of storage.

  • Use TIMESTAMP WITH TIME ZONE if you need to keep track of the session time zone in which the data was entered.

  • Use TIMESTAMP WITH LOCAL TIME ZONE if you want the database to automatically convert a time between the database and session time zones.

  • Use DATE when it’s necessary to maintain compatibility with an existing application written before any of the TIMESTAMP datatypes were introduced.

  • Use datatypes in your PL/SQL code that correspond to, or are at least compatible with, the underlying database tables. Think twice, for example, before reading a TIMESTAMP value from a table into a DATE variable, because you might lose information (in this case, the fractional seconds and perhaps the time zone).

Getting the current date and time. PL/SQL developers often need to retrieve and work with the current date and time. Most developers use the classic SYSDATE function, but Oracle Database now offers several functions to provide variations of this information, as shown in Table 1.

Function Time Zone Datatype Returned
CURRENT_DATE Session DATE
CURRENT_TIMESTAMP Session TIMESTAMP WITH TIME ZONE
LOCALTIMESTAMP Session TIMESTAMP
SYSDATE Database server DATE
SYSTIMESTAMP Database server TIMESTAMP WITH TIME ZONE

Table 1: SYSDATE and other options for working with the current date and time

Listing 2 displays the values returned by calls to SYSDATE and SYSTIMESTAMP.

Code Listing 2: Calls to SYSDATE and SYSTIMESTAMP and the returned values

BEGIN
DBMS_OUTPUT.put_line (SYSDATE);
DBMS_OUTPUT.put_line (SYSTIMESTAMP);
DBMS_OUTPUT.put_line (SYSDATE - SYSTIMESTAMP);
END;
/

Here is the output:

07-AUG-11
07-AUG-11 08.46.16.379000000 AM -05:00
-000000000 00:00:00.379000000

Because I have passed dates and time stamps to DBMS_OUTPUT.PUT_LINE, Oracle Database implicitly converts them to strings, using the default format masks for the database or the session (as specified by the National Language Settings NLS_DATE_FORMAT parameter). A default installation of Oracle Database sets the default DATE format to DD-MON-YYYY. The default TIMESTAMP format includes both the date offset and the time zone offset.

Note that it is possible to perform date arithmetic: I subtract the value returned by SYSTIMESTAMP from the value returned by SYSDATE. The result is an interval that is very close (but not quite equal) to zero.

Converting dates to strings and strings to dates. As with TO_CHAR for numbers, you use another version of the TO_CHAR function to convert a date or a time stamp to a string. And, again as with numbers, Oracle Database offers a large set of format elements to help you tweak that string so it appears exactly as you need it. Here are some examples:

  1. Use TO_CHAR without a format mask. If you do not include a format mask, the string returned by TO_CHAR will be the same as that returned when Oracle Database performs an implicit conversion:

    BEGIN
    DBMS_OUTPUT.put_line (
    TO_CHAR (SYSDATE));
    DBMS_OUTPUT.put_line (
    TO_CHAR (SYSTIMESTAMP));
    END;
    /
    07-AUG-11
    07-AUG-11 08.55.00.470000000 AM -05:00
  2. Use TO_CHAR to display the full names of both the day and the month in the date:

    BEGIN
    DBMS_OUTPUT.put_line (
    TO_CHAR (SYSDATE,
    'Day, DDth Month YYYY'));
    END;
    /
    Sunday , 07TH August 2011

    Note: The language used to display these names is determined by the NLS_DATE_LANGUAGE setting, which can also be specified as the third argument in the call to TO_CHAR, as in

    BEGIN
    DBMS_OUTPUT.put_line (
    TO_CHAR (SYSDATE,
    'Day, DDth Month YYYY',
    'NLS_DATE_LANGUAGE=Spanish'));
    END;
    /
    Domingo , 07TH Agosto 2011
  3. Use TO_CHAR to display the full names of both the day and the month in the date—but without all those extra spaces in the date-as-string. Oracle Database, by default, pads the string with spaces to match the maximum length of the day or the month. In most situations, you don’t want to include that extra text, and Oracle Database offers a format element modifier, FM, to control blank and zero padding. In the following block, I prefix the format mask with FM and remove the 0 (before 7) and extra spaces after August:
    BEGIN
    DBMS_OUTPUT.put_line (
    TO_CHAR (SYSDATE,
    'FMDay, DDth Month YYYY'));
    END;
    /
    Sunday, 7TH August 2011

You can also use the format mask to extract just a portion of, or information about, the date, as shown in the following examples:

  1. What quarter is it?

    TO_CHAR (SYSDATE, 'Q')
    
  2. What is the day of the year (1-366) for today’s date? 
     
    TO_CHAR (SYSDATE, 'DDD')
     
  3. What are the date and time of a DATE variable? (This is a very common requirement, because the default format mask for a date does not include the time component, which means that asking DBMS_OUTPUT.PUT_LINE to display a date leaves out the time.) 
    BEGIN
    DBMS_OUTPUT.put_line (
    TO_CHAR (SYSDATE,
    'YYYY-MM-DD HH24:MI:SS'));
    END;
    /

You can also use EXTRACT to extract and return the value of a specified element of a date. For example

  1. What year is it?
    EXTRACT (YEAR FROM SYSDATE)
    
  2. What is the day for today’s date?
    EXTRACT (DAY FROM SYSDATE)
    

To convert a string to a date, use the TO_DATE or the TO_TIMESTAMP built-in function. Provide the string and Oracle Database returns a date or a time stamp, using the default format mask for the session:

DECLARE
l_date DATE;
BEGIN
l_date := TO_DATE ('12-JAN-2011');
END ;

If the string you provide does not match the default format, Oracle Database will raise an exception:

DECLARE
l_date DATE;
BEGIN
l_date := TO_DATE ('January 12 2011');
END;
/ ORA-01858: a non-numeric character was
found where a numeric was expected

You should not assume that the literal value you provide in your call to TO_DATE matches the default format. What if the format changes over time? Instead, always provide a format mask when converting strings to dates, as in

l_date := TO_DATE ('January 12 2011',
'Month DD YYYY');

Date truncation. Use the TRUNC built-in function to truncate a date to the specified unit of measure. The most common use of TRUNC is TRUNC (date)—without any format mask specified. In this case, TRUNC simply sets the time to 00:00:00. You can also use TRUNC to easily obtain the first day in a specified period. Here are some TRUNC examples:

  1. Set l_date to today’s date, but with the time set to 00:00:00:

    l_date := TRUNC (SYSDATE); 
  2. Get the first day of the month for the specified date:
    l_date := TRUNC (SYSDATE, 'MM');
    
  3. Get the first day of the quarter for the specified date:
    l_date := TRUNC (SYSDATE, 'Q');
    
  4. Get the first day of the year for the specified date:
    l_date := TRUNC (SYSDATE, 'Y');
    

Date arithmetic. Oracle Database enables you to perform arithmetic operations on dates and time stamps in several ways:

  • 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.

  • Add one date to or subtract it from another, as in 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.

Here are some examples of date arithmetic with a date and a number (assume in all cases that the l_date variable has been declared as DATE):

  1. Set a local variable to tomorrow’s date:

    l_date := SYSDATE + 1;
    
  2. Move back one hour:
    l_date := SYSDATE - 1/24;
    
  3. Move ahead 10 seconds:
    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:

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;

Oracle Database offers several built-in functions for shifting a date by the requested amount or finding a date:

  • ADD_MONTHS—adds the specified number of months to or subtracts it from a date (or a time stamp)

  • NEXT_DAY—returns the date of the first weekday named in the call to the function

  • LAST_DAY—returns the date of the last day of the month of the specified date

Here are some examples that use these built-in functions:

  1. Move ahead one month:

    l_date := ADD_MONTHS (SYSDATE, 1);
    
  2. Move backward three months:

    l_date := ADD_MONTHS (SYSDATE, -3);  
  3. Starting with the last day of January, move ahead one month. Starting from a different date, go back one month. Starting with the last day of February, go back one month. Listing 3 shows three different calls to the ADD_MONTHS function along with the results. 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 

    You might be surprised at the third date in Listing 3. The first date (28 February) makes perfect sense. There is no 31st day in February, so Oracle Database returns the last day of the month. The second call to ADD_MONTHS moves the date from 27 February to 27 January: exactly one month’s change. But in the third call to ADD_MONTHS, Oracle Database notices that 28 February is the last day of the month, so it returns the last day of the month specified by the second argument.

  4. Find the next Saturday after today’s date:

    l_date := NEXT_DAY (SYSDATE, 'SAT');
    -- or
    l_date := NEXT_DAY (SYSDATE, 'SATURDAY');

The second argument must be a day of the week in the date language of your session (specified by NLS_DATE_LANGUAGE), provided as either the full name or the abbreviation. The returned date has the same time component as the date.

-------------------------------

present  by  dylan.

Working with Dates in PL/SQL(PL/SQL中使用日期)的更多相关文章

  1. 【SQL Server】中的日期函数和日期数据类型

    SQL Server Date 函数 SQL Server的重要日期函数包括: 函数 描述 参数含义 GETDATE() 返回当前的日期和时间   DATEPART(datepart,date)  返 ...

  2. pl/sql和sql的区别

    源地址:https://zhidao.baidu.com/question/187511430.html 1 sql(数据定义语言) 和PL/Sql的区别:答:SQL是结构化查询语言,比较接近自然语言 ...

  3. PL/SQL --> 动态SQL调用包中函数或过程

    动态SQL主要是用于针对不同的条件或查询任务来生成不同的SQL语句.最常用的方法是直接使用EXECUTE IMMEDIATE来执行动态SQL语句字符串或字符串变量.但是对于系统自定义的包或用户自定的包 ...

  4. PL/SQL将sql脚本数据导入Oracle

    PL/SQL将sql脚本数据导入数据库: 1.首先,使用plsql登录到需要导入数据的数据库.在[tools]--[Import tables] 2.选择第二个[SQL Inserts],在下面,点击 ...

  5. SQL PL/SQL语法手册

    SQL  PL/SQL语法手册 目   录 第一部分  SQL语法部分 3 一. CREATE TABLE 语句 3 二. CREATE SEQUENCE语句 5 三. CREATE VIEW语句 6 ...

  6. oracle中的sql%rowcount,sql%found、sql%notfound、sql%rowcount和sql%isopen

     Oracle 存储过程 删除表记录时删除不存在的记录也是显示删除成功 create or replace procedure delDept(p_deptno in dept.deptno%type ...

  7. 转://使用showplan.sql分析sql Performance

    在HelloDBA网站找到一个分析sql性能的工具—showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.z ...

  8. [转载]Oracle数据库 sql%found,sql%notfound,sql%rowcount

    sql%found,sql%notfound,sql%rowcount 在执行DML(insert,update,delete)语句时,可以用到以下三个隐式游标(游标是维护查询结果的内存中的一个区域, ...

  9. sql%found sql%notfound sql%rowcount sql%isopen

    原文引入:http://blog.csdn.net/mh942408056/article/details/6949325 sql%found sql%notfound sql%rowcount sq ...

  10. [转载]SQL语句中的日期计算

    1. 本月的第一天SELECT  DATEADD(mm,  DATEDIFF(mm,0,getdate()),  0) 2. 本月的最后一天SELECT  dateadd(ms,-3,DATEADD( ...

随机推荐

  1. [转帖]Web技术(五):HTTP/2 是如何解决HTTP/1.1 性能瓶颈的?

    文章目录 一.HTTP/2 概览 二.HTTP/2 协议原理 2.1 Binary frame layer 2.1.1 DATA帧定义 2.1.2 HEADERS帧定义 2.2 Streams and ...

  2. [转帖]TiDB BR 备份至 MinIO S3 实战

    https://tidb.net/blog/3a31d41d#3.%E9%83%A8%E7%BD%B2%20MinIO%20S3%20%E5%8F%8A%E5%A4%87%E4%BB%BD%E6%81 ...

  3. [转帖]elasticsearch-create-enrollment-tokenedit

    https://www.elastic.co/guide/en/elasticsearch/reference/current/create-enrollment-token.html The ela ...

  4. [转帖]集群监控之 —— ipmi操作指南

    https://www.cnblogs.com/gaoyuechen/p/8506930.html 这两天,配置了一堆500来个节点的大型集群,被ipmi的问题困扰了一天半,到下午16:40,终于解决 ...

  5. Intel 第四代志强可扩展SKU

  6. 高性能MySQL实战(三):性能优化 | 京东物流技术团队

    这篇主要介绍对慢 SQL 优化的一些手段,而在讲解具体的优化措施之前,我想先对 EXPLAIN 进行介绍,它是我们在分析查询时必要的操作,理解了它输出结果的内容更有利于我们优化 SQL.为了方便大家的 ...

  7. 从零开始配置 vim(9)——初始配置

    虽然本系列文章叫做从0开始配置vim,似乎我们从一开始就要写vimrc配置文件,但是我们并没有这么做.我们先经过几篇文章了解了下面的几个内容 如何设置vim属性,从而改变vim的特征 配置快捷键,以提 ...

  8. 大语言模型的预训练4:指示学习Instruction Learning详解以及和Prompt Learning,In-content Learning区别

    大语言模型的预训练[4]:指示学习Instruction Learning:Entailment-oriented.PLM oriented.human-oriented详解以及和Prompt Lea ...

  9. PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】

    相关项目链接: Paddlenlp之UIE模型实战实体抽取任务[打车数据.快递单] Paddlenlp之UIE分类模型[以情感倾向分析新闻分类为例]含智能标注方案) 应用实践:分类模型大集成者[Pad ...

  10. 强化学习从基础到进阶-常见问题和面试必知必答[8]:近端策略优化(proximal policy optimization,PPO)算法

    强化学习从基础到进阶-常见问题和面试必知必答[8]:近端策略优化(proximal policy optimization,PPO)算法 1.核心词汇 同策略(on-policy):要学习的智能体和与 ...