跳到主要内容

Date & Time Functions

This page provides a comprehensive overview of Date & Time functions in Databend, organized by functionality for easy reference.

Current Date & Time Functions

FunctionDescriptionExample
NOWReturns the current date and timeNOW()2024-06-04 17:42:31.123456
CURRENT_TIMESTAMPReturns the current date and timeCURRENT_TIMESTAMP()2024-06-04 17:42:31.123456
TODAYReturns the current dateTODAY()2024-06-04
TOMORROWReturns tomorrow's dateTOMORROW()2024-06-05
YESTERDAYReturns yesterday's dateYESTERDAY()2024-06-03

Date & Time Extraction Functions

FunctionDescriptionExample
YEARExtracts the year from a dateYEAR('2024-06-04')2024
MONTHExtracts the month from a dateMONTH('2024-06-04')6
DAYExtracts the day from a dateDAY('2024-06-04')4
QUARTERExtracts the quarter from a dateQUARTER('2024-06-04')2
WEEK / WEEKOFYEARExtracts the week number from a dateWEEK('2024-06-04')23
EXTRACTExtracts a part from a dateEXTRACT(MONTH FROM '2024-06-04')6
DATE_PARTExtracts a part from a dateDATE_PART('month', '2024-06-04')6
YEARWEEKReturns year and week numberYEARWEEK('2024-06-04')202423
MILLENNIUMReturns the millennium from a dateMILLENNIUM('2024-06-04')3

Date & Time Conversion Functions

FunctionDescriptionExample
DATEConverts a value to DATE typeDATE('2024-06-04')2024-06-04
TO_DATEConverts a string to DATE typeTO_DATE('2024-06-04')2024-06-04
TO_DATETIMEConverts a string to DATETIME typeTO_DATETIME('2024-06-04 12:30:45')2024-06-04 12:30:45
TO_TIMESTAMPConverts a string to TIMESTAMP typeTO_TIMESTAMP('2024-06-04 12:30:45')2024-06-04 12:30:45
TO_UNIX_TIMESTAMPConverts a date to Unix timestampTO_UNIX_TIMESTAMP('2024-06-04')1717516800
TO_YYYYMMFormats date as YYYYMMTO_YYYYMM('2024-06-04')202406
TO_YYYYMMDDFormats date as YYYYMMDDTO_YYYYMMDD('2024-06-04')20240604
TO_YYYYMMDDHHFormats date as YYYYMMDDHHTO_YYYYMMDDHH('2024-06-04 12:30:45')2024060412
TO_YYYYMMDDHHMMSSFormats date as YYYYMMDDHHMMSSTO_YYYYMMDDHHMMSS('2024-06-04 12:30:45')20240604123045
DATE_FORMATFormats a date according to a format stringDATE_FORMAT('2024-06-04', '%Y-%m-%d')'2024-06-04'

Date & Time Arithmetic Functions

FunctionDescriptionExample
DATE_ADDAdds a time interval to a dateDATE_ADD(DAY, 7, '2024-06-04')2024-06-11
DATE_SUBSubtracts a time interval from a dateDATE_SUB(MONTH, 1, '2024-06-04')2024-05-04
ADD INTERVALAdds an interval to a date'2024-06-04' + INTERVAL 1 DAY2024-06-05
SUBTRACT INTERVALSubtracts an interval from a date'2024-06-04' - INTERVAL 1 MONTH2024-05-04
DATE_DIFFReturns the difference between two datesDATE_DIFF(DAY, '2024-06-01', '2024-06-04')3
TIMESTAMP_DIFFReturns the difference between two timestampsTIMESTAMP_DIFF(HOUR, '2024-06-04 10:00:00', '2024-06-04 15:00:00')5
MONTHS_BETWEENReturns the number of months between two datesMONTHS_BETWEEN('2024-06-04', '2024-01-04')5
DATE_BETWEENChecks if a date is between two other datesDATE_BETWEEN('2024-06-04', '2024-06-01', '2024-06-10')true

Date & Time Truncation Functions

FunctionDescriptionExample
DATE_TRUNCTruncates a timestamp to a specified precisionDATE_TRUNC('month', '2024-06-04')2024-06-01
TO_START_OF_DAYReturns the start of the dayTO_START_OF_DAY('2024-06-04 12:30:45')2024-06-04 00:00:00
TO_START_OF_HOURReturns the start of the hourTO_START_OF_HOUR('2024-06-04 12:30:45')2024-06-04 12:00:00
TO_START_OF_MINUTEReturns the start of the minuteTO_START_OF_MINUTE('2024-06-04 12:30:45')2024-06-04 12:30:00
TO_START_OF_MONTHReturns the start of the monthTO_START_OF_MONTH('2024-06-04')2024-06-01
TO_START_OF_QUARTERReturns the start of the quarterTO_START_OF_QUARTER('2024-06-04')2024-04-01
TO_START_OF_YEARReturns the start of the yearTO_START_OF_YEAR('2024-06-04')2024-01-01
TO_START_OF_WEEKReturns the start of the weekTO_START_OF_WEEK('2024-06-04')2024-06-03

Date & Time Navigation Functions

FunctionDescriptionExample
LAST_DAYReturns the last day of the monthLAST_DAY('2024-06-04')2024-06-30
NEXT_DAYReturns the date of the next specified day of weekNEXT_DAY('2024-06-04', 'SUNDAY')2024-06-09
PREVIOUS_DAYReturns the date of the previous specified day of weekPREVIOUS_DAY('2024-06-04', 'MONDAY')2024-06-03

Other Date & Time Functions

FunctionDescriptionExample
TIMEZONEReturns the current timezoneTIMEZONE()'UTC'
TIME_SLOTReturns time slotsTIME_SLOT('2024-06-04 12:30:45', 15, 'MINUTE')2024-06-04 12:30:00