Updated on 2025-05-29 GMT+08:00

EXTRACT

EXTRACT(field FROM source)

Description: The extract function retrieves fields such as year or hour from date/time values.

Parameters:

  • source is a value expression of the timestamp, time, or interval type. (Expressions of the date type are cast to timestamp and can therefore be used as well.)
  • field is an identifier or string that selects what field to extract from the source value.

Return type: double precision

The following are valid field names:

  • century

    Century.

    The first century starts at 0001-01-01 00:00:00 AD. This definition applies to all Gregorian calendar countries. There is no century number 0. You go from –1 century to 1 century.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
     date_part 
    -----------
            20
    (1 row)
    
  • day
    • For timestamp values, the day (of the month) field (1–31).
      1
      2
      3
      4
      5
      gaussdb=# SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
       date_part 
      -----------
              16
      (1 row)
      
    • For interval values, the number of days
      1
      2
      3
      4
      5
      gaussdb=# SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
       date_part 
      -----------
              40
      (1 row)
      
  • decade

    Year divided by 10

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
           200
    (1 row)
    
  • dow

    Day of the week as Sunday (0) to Saturday (6)

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
             5
    (1 row)
    
  • doy

    Day of the year (1–365 or 366)

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
            47
    (1 row)
    
  • epoch
    • For timestamp with time zone values, the number of seconds since 1970-01-01 00:00:00-00 UTC (can be negative).

      For date and timestamp values, the number of seconds since 1970-01-01 00:00:00-00 local time.

      For interval values, the total number of seconds in the interval.

      1
      2
      3
      4
      5
      gaussdb=# SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
        date_part   
      --------------
       982384720.12
      (1 row)
      
      1
      2
      3
      4
      5
      gaussdb=# SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
       date_part 
      -----------
          442800
      (1 row)
      
    • Way to convert an epoch value back to a timestamp
      1
      2
      3
      4
      5
      gaussdb=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720.12 * INTERVAL '1 second' AS RESULT;
                result          
      ---------------------------
       2001-02-17 12:38:40.12+08
      (1 row)
      
  • hour

    Hour (0–23).

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
            20
    (1 row)
    
  • isodow

    Day of the week (1–7).

    Monday is 1 and Sunday is 7.

    This is identical to dow except for Sunday.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
     date_part 
    -----------
             7
    (1 row)
    
  • isoyear

    The ISO 8601 year that the date falls in (not applicable to intervals).

    Each ISO year begins with the Monday of the week containing January 4, so in early January or late December the ISO year may be different from the Gregorian year. For details, see week.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    gaussdb=# SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-01');
     date_part 
    -----------
          2005
    (1 row)
    gaussdb=# SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-01-01 00:00:40');
     date_part
    -----------
            52
    (1 row)
    
    gaussdb=# SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
     date_part 
    -----------
          2006
    (1 row)
    gaussdb=# SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-01-02 00:00:40');
     date_part
    -----------
             1
    (1 row)
  • microseconds

    The second field, including fractional parts, is multiplied by 1,000,000.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
     date_part 
    -----------
      28500000
    (1 row)
    
  • millennium

    Millennium.

    Years in the 1900s are in the second millennium. The third millennium started from January 1, 2001.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
             3
    (1 row)
    
  • milliseconds

    Second field, including fractional parts, is multiplied by 1000. Note that this includes full seconds.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
     date_part 
    -----------
         28500
    (1 row)
    
  • minute

    Minute (0–59).

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
            38
    (1 row)
    
  • month

    For timestamp values, the specific month in the year (1–12).

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
             2
    (1 row)
    

    For interval values, the number of months, modulo 12 (0–11).

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
     date_part 
    -----------
             1
    (1 row)
    
  • quarter

    Quarter of the year (1–4) that the date is in.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
             1
    (1 row)
    
  • second

    Seconds field, including fractional parts (0–59).

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
     date_part 
    -----------
          28.5
    (1 row)
    
  • timezone

    Time zone offset from UTC, measured in seconds. Positive values correspond to time zones east of UTC, negative values to zones west of UTC.

  • timezone_hour

    Hour component of the time zone offset.

  • timezone_minute

    Minute component of the time zone offset.

  • week

    Number of the week of the year that the day is in. According to the ISO-8601 standard, the first week of a year contains January 4 of the year (the week of the ISO-8601 standard starts from Monday). In other words, the first Thursday of a year is in week 1 of that year.

    Because of this, it is possible for early January dates to be part of the 52nd or 53rd week of the previous year, and late December dates to be part of the 1st week of the next year. For example, 2006-01-01 is the 52nd week of 2005, and 2006-01-02 is the first week of 2006. You are advised to use the columns isoyear and week together to ensure consistency.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    gaussdb=# SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-01');
     date_part 
    -----------
          2005
    (1 row)
    gaussdb=# SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-01-01 00:00:40');
     date_part
    -----------
            52
    (1 row)
    
    gaussdb=# SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
     date_part 
    -----------
          2006
    (1 row)
    gaussdb=# SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-01-02 00:00:40');
     date_part
    -----------
             1
    (1 row)
  • year

    Year field.

    1
    2
    3
    4
    5
    gaussdb=# SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
     date_part 
    -----------
          2001
    (1 row)