DATEADD
Description
The alias function of TIMESTAMPADD
is used to add or subtract a specified number of time units unit
to a given timestamp ts
. If count
is positive, it means addition; if negative, it means subtraction.
Acceptable time units unit
include (case insensitive):
- MICROSECOND
- MILLISECOND
- SECOND
- MINUTE
- HOUR
- DAY
- DAYOFYEAR
- WEEK
- MONTH
- QUARTER
- YEAR
Parameter Description
unit
: timestamp_ltz type, representing the time unit to be added.count
: bigint type, representing the number to be added or subtracted.ts
: timestamp_ltz type, representing the current timestamp.
Return Type
Returns a new timestamp_ltz type timestamp.
Example
- Add 1 year:
- Reduce by 30 days:
- Add 12 hours:
- Reduce by 3 months:
- Add 2 milliseconds:
Notes
- When handling timestamps, ensure that the parameter format is correct, otherwise it may cause the function to fail.
- When converting time units, pay attention to special cases such as leap years and the number of days in a month to avoid incorrect results.
- Please choose the appropriate time unit and increment according to actual needs to avoid unnecessary time errors.