Updated on 2026-11-06 GMT+08:00

datepart1

The datepart1 function is used to calculate the value from the date that matches the specified time unit datepart.

Syntax

datepart1(string date,string datepart)

Parameters

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

date

Yes

DATE

or

STRING

The start date.

Formats:

  • yyyy-mm-dd
  • yyyy-mm-dd hh:mi:ss
  • yyyy-mm-dd hh:mi:ss.ff3

datepart

Yes

STRING

The time unit to return.

The datepart parameter supports extended date formats: year, month or mon, day, and hour.

  • yyyy indicates year.
  • mm indicates month.
  • dd indicates day.
  • hh indicates hour.
  • mi indicates minute.
  • ss indicates second.

Return Values

Returns a value of type BIGINT.

  • If date is not of type DATE or STRING, an error is returned with the message "data type mismatch".
  • If date is of type DATE or STRING but does not conform to the input format for date values, NULL is returned.
  • If the date value is NULL, NULL is returned.
  • If the datepart value is NULL, NULL is returned.

Example Code

Returns 2023.

select datepart1(date '2023-08-14 17:00:00', 'yyyy');

Returns 2023.

select datepart1('2023-08-14 17:00:00', 'yyyy');

Returns 59.

select datepart1('2023-08-14 17:59:59', 'mi')

Returns NULL.

select datepart1(date '2023-08-14', null);