Date and Time Processing Functions and Operators

Date and Time Operators

When the user uses date/time operators, explicit type prefixes are modified for corresponding operands to ensure that the operands parsed by the database are consistent with what the user expects, and no unexpected results occur.

For example, abnormal mistakes will occur in the following example without an explicit data type.

1
SELECT date '2001-10-01' - '7' AS RESULT;
Table 1 Time and date operators

Operators

Examples

+

Add a date with an integer to obtain the date after 7 days.

1
2
3
4
5
SELECT date '2001-09-28' + integer '7' AS RESULT;
       result        
---------------------
 2001-10-05 00:00:00
(1 row)

Add a date with an interval to obtain the time after 1 hour.

1
2
3
4
5
SELECT date '2001-09-28' + interval '1 hour' AS RESULT;
       result        
---------------------
 2001-09-28 01:00:00
(1 row)

Add a date with a time to obtain a specific time.

1
2
3
4
5
SELECT date '2001-09-28' + time '03:00' AS RESULT;
       result        
---------------------
 2001-09-28 03:00:00
(1 row)

Add a date with an interval to obtain the time after one month.

If the sum or subtraction results fall beyond the date range of a month, the result will be rounded to the last day of the month.

1
2
3
4
5
SELECT date '2021-01-31' + interval '1 month' AS RESULT;
       result
---------------------
 2021-02-28 00:00:00
(1 row)
1
2
3
4
5
SELECT date '2021-02-28' + interval '1 month' AS RESULT;
       result
---------------------
 2021-03-28 00:00:00
(1 row)

Add two intervals to obtain the sum.

1
2
3
4
5
SELECT interval '1 day' + interval '1 hour' AS RESULT;
     result     
----------------
 1 day 01:00:00
(1 row)

Add a timestamp with an interval to obtain the time after 23 hours.

1
2
3
4
5
SELECT timestamp '2001-09-28 01:00' + interval '23 hours' AS RESULT;
       result        
---------------------
 2001-09-29 00:00:00
(1 row)

Add a time with an interval to obtain the time after three hours.

1
2
3
4
5
SELECT time '01:00' + interval '3 hours' AS RESULT;
  result  
----------
 04:00:00
(1 row)

-

Subtract a date from another to obtain the difference.

1
2
3
4
5
SELECT date '2001-10-01' - date '2001-09-28' AS RESULT;
 result 
--------
 3 days
(1 row)

Subtract an integer from a date, the return is a timestamp type.

1
2
3
4
5
SELECT date '2001-10-01' - integer '7' AS RESULT;
       result        
---------------------
 2001-09-24 00:00:00
(1 row)

Subtract an interval from a date to obtain the time difference.

1
2
3
4
5
SELECT date '2001-09-28' - interval '1 hour' AS RESULT;
       result        
---------------------
 2001-09-27 23:00:00
(1 row)

Subtract a time from another time to obtain the time difference.

1
2
3
4
5
SELECT time '05:00' - time '03:00' AS RESULT;
  result  
----------
 02:00:00
(1 row)

Subtract an interval from a time to obtain the time difference.

1
2
3
4
5
SELECT time '05:00' - interval '2 hours' AS RESULT;
  result  
----------
 03:00:00
(1 row)

Subtract an interval from a timestamp to obtain the date difference.

1
2
3
4
5
SELECT timestamp '2001-09-28 23:00' - interval '23 hours' AS RESULT;
       result        
---------------------
 2001-09-28 00:00:00
(1 row)

Subtract an interval from another interval to obtain the time difference.

1
2
3
4
5
SELECT interval '1 day' - interval '1 hour' AS RESULT;
  result  
----------
 23:00:00
(1 row)

Subtract a timestamp from another timestamp to obtain the time difference.

1
2
3
4
5
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' AS RESULT;
     result     
----------------
 1 day 15:00:00
(1 row)

Obtain the time at the previous day.

1
2
3
4
5
select now() - interval '1 day'AS RESULT;
           result
-------------------------------
 2022-08-08 01:46:15.555406+00
(1 row)

*

Multiply an interval by a quantity:

1
2
3
4
5
SELECT 900 * interval '1 second' AS RESULT;
  result  
----------
 00:15:00
(1 row)
1
2
3
4
5
SELECT 21 * interval '1 day' AS RESULT;
 result  
---------
 21 days
(1 row)
1
2
3
4
5
SELECT double precision '3.5' * interval '1 hour' AS RESULT;
  result  
----------
 03:30:00
(1 row)

/

Divide an interval by a quantity to obtain a time segment.

1
2
3
4
5
SELECT interval '1 hour' / double precision '1.5' AS RESULT;
  result  
----------
 00:40:00
(1 row)

Time/Date functions

EXTRACT

EXTRACT(field FROM source)

The extract function retrieves subcolumns such as year or hour from date/time values. source must be a value expression of type timestamp, time, or interval. (Expressions of type date are cast to timestamp and can therefore be used as well.) field is an identifier or string that selects what column to extract from the source value. The extract function returns values of type double precision. The following are valid field names:

date_part

The date_part function is modeled on the traditional Ingres equivalent to the SQL-standard function extract:

date_part('field', source)

Note that the field must be a string, rather than a name. The valid field names are the same as those for extract. For details, see EXTRACT.

For example:

1
2
3
4
5
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
 date_part 
-----------
        16
(1 row)
1
2
3
4
5
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
 date_part 
-----------
         4
(1 row)

date_format

date_format(timestamp, fmt)

Converts a date into a string in the format specified by fmt.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
SELECT date_format('2009-10-04 22:23:00', '%M %D %W');
    date_format
--------------------
 October 4th Sunday
(1 row)
SELECT date_format('2021-02-20 08:30:45', '%Y-%m-%d %H:%i:%S');
     date_format
---------------------
 2021-02-20 08:30:45
(1 row)
SELECT date_format('2021-02-20 18:10:15', '%r-%T');
     date_format
----------------------
 06:10:15 PM-18:10:15
(1 row)

The following table describes the patterns of date parameter values. They can be used for the date_format, time_format, str_to_date, str_to_time, and from_unixtime functions.

Table 2 Output formats of date_format

Format

Description

Value

%a

Abbreviated week name

Sun...Sat

%b

Abbreviated month name

Jan...Dec

%c

Month

0...12

%D

Date with a suffix

0th, 1st, 2nd, 3rd, ...

%d

Day in a month (two digits)

00...31

%e

Day in a month

0...31

%f

Microsecond

000000...999999

%H

Hour, in 24-hour format

00...23

%h

Hour, in 12-hour format

01...12

%I

Hour, in 12-hour format, same as %h

01...12

%i

Minute

00...59

%j

Day in a year

001...366

%k

Hour, in 24-hour format, same as %H

0...23

%l

Hour, in 12-hour format, same as %h

1...12

%M

Month name

January...December

%m

Month (two digits)

00...12

%p

Morning and afternoon

AM PM

%r

Time, in 12-hour format

hh::mm::ss AM/PM

%S

Second

00...59

%s

Second, same as %S

00...59

%T

Time, in 24-hour format

hh::mm::ss

%U

Week (Sunday is the first day of a week)

00...53

%u

Week (Monday is the first day of a week)

00...53

%V

Week (Sunday is the first day of a week). It is used together with %X.

01...53

%v

Week (Monday is the first day of a week). It is used together with %x.

01...53

%W

Week name

Sunday...Saturday

%w

Day of a week. The value is 0 for Sunday.

0...6

%X

Year (four digits). It is used together with %V. Sunday is the first day of a week.

-

%x

Year (four digits). It is used together with %v. Monday is the first day of a week.

-

%Y

Year (four digits)

-

%y

Year (two digits)

-

%%

Character '%'

Character '%'

%x

'x': any character apart from the preceding ones

Character 'x'

In the preceding table, %U, %u, %V, %v, %X, and %x are not supported currently.