Time Functions
Overview
Time functions are used to perform operations on data of the DATE, TIME, and TIMESTAMP types, including type conversion, obtaining the current time, extracting time components, formatting, rounding time, and calculating time offsets.
For more information, see Temporal Functions.
Application Scenarios
- Watermark definition: The CURRENT_TIMESTAMP and INTERVAL functions are used to define the watermark policy.
- Window grouping: The DATE_FORMAT function is used to format the time into hour- or day-level for window grouping.
- Time calculation: The TIMESTAMPADD and TIMESTAMPDIFF functions are used to calculate the time offset and difference.
- ETL time field processing: The year, month, and day components are extracted for data partitioning and aggregation.
Notes and Constraints
- CURRENT_TIMESTAMP returns the timestamp in the UTC time zone, and LOCALTIMESTAMP returns the timestamp in the local time zone.
- DATE_FORMAT uses the Java DateTimeFormatter format pattern (such as yyyy-MM-dd and HH:mm:ss), which is different from the SimpleDateFormat pattern.
- INTERVAL supports only the year and month interval (YEAR and MONTH) and the day, hour, minute, and second interval (DAY, HOUR, MINUTE, and SECOND). These intervals cannot be used together.
- PROCTIME() can be used only to declare the processing time attribute in DDL statements and cannot be directly called in queries.
Syntax Structure of Type Conversion
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| DATE string | DATE | Converts a string to the DATE type. The string parameter must be of the DATE or STRING type and in the format of yyyy-MM-dd. Otherwise, NULL is returned. The return value is of the DATE type. | SELECT DATE '2024-01-15'; -- 2024-01-15 |
| TIME string | TIME | Converts a string to the TIME type. The string parameter must be of the TIME or STRING type and in the format of HH:mm:ss. Otherwise, NULL is returned. The return value is of the TIME type. | SELECT TIME '10:30:00'; -- 10:30:00 |
| TIMESTAMP string | TIMESTAMP | Converts a string to the TIMESTAMP type. The string parameter must be of the TIMESTAMP or STRING type and in the format of yyyy-MM-dd HH:mm:ss[.SSSSSS]. Otherwise, NULL is returned. The return value is of the TIMESTAMP type. | SELECT TIMESTAMP '2024-01-15 10:30:00'; -- 2024-01-15 10:30:00 |
| INTERVAL string range | INTERVAL | Converts a string to an interval. The string parameter is of the STRING type and indicates the interval. The range parameter is a unit qualifier, which can be YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, YEAR TO MONTH, or DAY TO SECOND. The return value is of the INTERVAL type. | SELECT INTERVAL '1' DAY; -- +1 00:00:00.000 |
| TO_DATE(string[, format]) | DATE | Converts a string to the DATE type in the specified format. The string parameter is of the STRING type. The format parameter is an optional Java DateTimeFormatter string, which defaults to yyyy-MM-dd. If the format does not match, NULL is returned. The return value is of the DATE type. | SELECT TO_DATE('2024-01-15', 'yyyy-MM-dd'); -- 2024-01-15 |
| TO_TIMESTAMP(string[, format]) | TIMESTAMP | Converts a string to the TIMESTAMP type in the specified format. The string parameter is of the STRING type. The format parameter is an optional Java DateTimeFormatter string, which defaults to yyyy-MM-dd HH:mm:ss. If the format does not match, NULL is returned. The return value is of the TIMESTAMP type. | SELECT TO_TIMESTAMP('2024-01-15 10:30:00', 'yyyy-MM-dd HH:mm:ss'); -- 2024-01-15 10:30:00 |
Syntax for Obtaining Current Time Class
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| CURRENT_DATE | DATE | Returns the date of the current SQL session in the UTC time zone. It takes no parameters. The return value is of the DATE type. | SELECT CURRENT_DATE; -- 2024-01-15 |
| CURRENT_TIME | TIME WITH TIME ZONE | Returns the time of the current SQL session with time zone. It takes no parameters. The return value is of the TIME WITH TIME ZONE type. | SELECT CURRENT_TIME; -- 10:30:00+08:00 |
| CURRENT_TIMESTAMP | TIMESTAMP_LTZ(3) | Returns the timestamp of the current SQL session with the local time zone and a precision of three milliseconds. It takes no parameters. The return value is of the TIMESTAMP_LTZ(3) type. | SELECT CURRENT_TIMESTAMP; -- 2024-01-15T10:30:00.000+08:00 |
| LOCALTIME | TIME | Returns the local time of the current SQL session (without time zone). It takes no parameters. The return value is of the TIME type. | SELECT LOCALTIME; -- 10:30:00 |
| LOCALTIMESTAMP | TIMESTAMP(3) | Returns the local timestamp of the current SQL session (without time zone, with millisecond precision of three digits). It takes no parameters. The return value is of the TIMESTAMP(3) type. | SELECT LOCALTIMESTAMP; -- 2024-01-15 10:30:00.000 |
| NOW() | TIMESTAMP_LTZ(3) | Returns the current timestamp, which is equivalent to CURRENT_TIMESTAMP. It takes no parameters. The return value is of the TIMESTAMP_LTZ(3) type. | SELECT NOW(); -- 2024-01-15T10:30:00.000+08:00 |
Syntax for Extracting Time Components
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| EXTRACT(timeintervalunit FROM temporal) | INT | Extracts the value of the specified time part from a time value. The timeintervalunit parameter is a time unit enumeration, which can be YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, QUARTER, WEEK, DOW, or DOY. The temporal parameter must be of the DATE, TIME, TIMESTAMP, or INTERVAL type. The return value is of the INT type. | SELECT EXTRACT(YEAR FROM TIMESTAMP '2024-01-15 10:30:00'); -- 2024 |
| YEAR(date) | INT | Extracts the year from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 to 9999. | SELECT YEAR(DATE '2024-01-15'); -- 2024 |
| QUARTER(date) | INT | Extracts the quarter from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 to 4. | SELECT QUARTER(DATE '2024-04-15'); -- 2 |
| MONTH(date) | INT | Extracts the month from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 to 12. | SELECT MONTH(DATE '2024-01-15'); -- 1 |
| WEEK(date) | INT | Extracts the week number of the year from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 to 53. | SELECT WEEK(DATE '2024-01-15'); -- 3 |
| DAYOFYEAR(date) | INT | Extracts the day number of the year from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 to 366. | SELECT DAYOFYEAR(DATE '2024-02-01'); -- 32 |
| DAYOFMONTH(date) | INT | Extracts the day number of the month from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 to 31. | SELECT DAYOFMONTH(DATE '2024-01-15'); -- 15 |
| DAYOFWEEK(date) | INT | Extracts the day of the week from a date. The date parameter must be of the DATE or TIMESTAMP type. The return value is of the INT type, ranging from 1 (Sunday) to 7 (Saturday). | SELECT DAYOFWEEK(DATE '2024-01-15'); -- 2 |
| HOUR(timestamp) | INT | Extracts the hour from the timestamp. The timestamp parameter must be of the TIME or TIMESTAMP type. The return value is of the INT type, ranging from 0 to 23. | SELECT HOUR(TIMESTAMP '2024-01-15 10:30:00'); -- 10 |
| MINUTE(timestamp) | INT | Extracts the minute from the timestamp. The timestamp parameter must be of the TIME or TIMESTAMP type. The return value is of the INT type, ranging from 0 to 59. | SELECT MINUTE(TIMESTAMP '2024-01-15 10:30:00'); -- 30 |
| SECOND(timestamp) | INT | Extracts the second from the timestamp. The timestamp parameter must be of the TIME or TIMESTAMP type. The return value is of the INT type, ranging from 0 to 59. | SELECT SECOND(TIMESTAMP '2024-01-15 10:30:45'); -- 45 |
Syntax for Rounding Time
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| FLOOR(temporal TO timeintervalunit) | Consistent with the input type | Rounds down the time value to the specified time unit. The temporal parameter must be of the DATE, TIME, or TIMESTAMP type. The timeintervalunit parameter indicates the rounding unit, which can be YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND. The return value type is the same as the input type. | SELECT FLOOR(TIMESTAMP '2024-01-15 10:30:45' TO HOUR); -- 2024-01-15 10:00:00 |
| CEIL(temporal TO timeintervalunit) | Consistent with the input type | Rounds up the time value to the specified time unit. The temporal parameter must be of the DATE, TIME, or TIMESTAMP type. The timeintervalunit parameter indicates the rounding unit, which can be YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND. The return value type is the same as the input type. | SELECT CEIL(TIMESTAMP '2024-01-15 10:30:45' TO HOUR); -- 2024-01-15 11:00:00 |
Time Formatting Class
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| DATE_FORMAT(timestamp, format) | STRING | Converts a timestamp to a string in the specified format. The timestamp parameter must be of the TIMESTAMP or TIMESTAMP_LTZ type. The format parameter is of the STRING type and uses the Java DateTimeFormatter format pattern (such as yyyy-MM-dd, HH:mm:ss, and yyyyMMdd). The return value is of the STRING type. | SELECT DATE_FORMAT(TIMESTAMP '2024-01-15 10:30:00', 'yyyy-MM-dd'); -- '2024-01-15' |
Time Calculation Class
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| TIMESTAMPADD(unit, interval, timestamp) | TIMESTAMP | Adds a specified interval to a timestamp. The unit parameter is the time unit, which can be YEAR, MONTH, QUARTER, WEEK, DAY, HOUR, MINUTE, or SECOND. The interval parameter must be of the INT type, indicating the number of intervals to be added (a negative number can be used to implement subtraction). The timestamp parameter must be of the TIMESTAMP type. The return value is of the TIMESTAMP type. | SELECT TIMESTAMPADD(DAY, 7, TIMESTAMP '2024-01-15 10:30:00'); -- 2024-01-22 10:30:00 |
| TIMESTAMPDIFF(unit, timestamp1, timestamp2) | INT | Calculates the difference between two timestamps. The unit parameter specifies the time unit, which can be YEAR, MONTH, QUARTER, WEEK, DAY, HOUR, MINUTE, or SECOND. The timestamp1 and timestamp2 parameters must be of the TIMESTAMP type. The return value is of the INT type, indicating the difference between timestamp2 and timestamp1 in the specified unit. | SELECT TIMESTAMPDIFF(DAY, TIMESTAMP '2024-01-01 00:00:00', TIMESTAMP '2024-01-15 00:00:00'); -- 14 |
| DATE_ADD | STRING | The DATE_ADD function returns the date after adding a specified number of days to a given date. If the target number of days is negative, the corresponding number of days is subtracted. | For details, see DATE_ADD. |
| DATE_SUB | STRING | The DATE_SUB function returns the date after subtracting a specified number of days from a given date. If the target number of days is negative, the corresponding number of days is added. | For details, see DATE_SUB. |
| DATEDIFF | INT | The DATEDIFF function calculates the day difference between two dates. The calculation subtracts startdate from enddate, meaning the return value equals enddate – startdate. A positive number is returned if enddate is later than startdate, and a negative number is returned if enddate is earlier than startdate. | For details, see DATEDIFF. |
Time Attribute Processing Class
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| PROCTIME() | TIMESTAMP_LTZ(3) | Declares the processing time attribute and returns the current processing timestamp. It takes no parameters. It can be used only as a column type declaration in the table definition of DDL and cannot be directly called in queries. The return value is of the TIMESTAMP_LTZ(3) type, indicating the machine time when the data is processed. | -- Using in DDL: proctime AS PROCTIME() |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot