Back to Materialize

date_part function

doc/user/content/sql/functions/date-part.md

1232.4 KB
Original Source

date_part returns some time component from a time-based value, such as the year from a Timestamp. It is mostly functionally equivalent to the function EXTRACT, except to maintain PostgreSQL compatibility, date_part returns values of type float. This can result in a loss of precision in certain uses. Using EXTRACT is recommended instead.

Signatures

{{% include-syntax file="examples/sql_functions/date_part" example="syntax" %}}

ParameterTypeDescription
valtime, timestamp, timestamp with time zone, interval, dateThe value from which you want to extract a component. vals of type date are first cast to type timestamp.

Arguments

date_part supports multiple synonyms for most time periods.

Time periodSynonyms
epochEPOCH
millenniumMIL, MILLENNIUM, MILLENNIA
centuryC, CENT, CENTURY, CENTURIES
decadeDEC, DECS, DECADE, DECADES
yearY, YEAR, YEARS, YR, YRS
quarterQTR, QUARTER
monthMON, MONS, MONTH, MONTHS
weekW, WEEK, WEEKS
dayD, DAY, DAYS
hourH, HR, HRS, HOUR, HOURS
minuteM, MIN, MINS, MINUTE, MINUTES
secondS, SEC, SECS, SECOND, SECONDS
microsecondUS, USEC, USECS, USECONDS, MICROSECOND, MICROSECONDS
millisecondMS, MSEC, MSECS, MSECONDS, MILLISECOND, MILLISECONDS
day of weekDOW
ISO day of weekISODOW
day of yearDOY

Return value

date_part returns a float value.

Examples

Extract second from timestamptz

mzsql
SELECT date_part('S', TIMESTAMP '2006-01-02 15:04:05.06');
nofmt
 date_part
-----------
      5.06

Extract century from date

mzsql
SELECT date_part('CENTURIES', DATE '2006-01-02');
nofmt
 date_part
-----------
        21