Modify Date and Time Values
1. Overview
The modify date and time value functions covered in this section are:
DATEADD
DATEADD function adds a specified number value to a specified datepart of an input date value, and then returns that modified value.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
Syntax
Arguments
datepart
The part of date to which DATEADD
adds an integer number. This table lists all valid datepart arguments.
datepart |
year |
quarter |
month |
dayofyear |
day |
week |
weekday |
hour |
minute |
second |
millisecond |
microsecond |
nanosecond |
number
An expression that can resolve to an int that DATEADD
adds to a datepart of date. DATEADD
accepts user-defined variable values for number. DATEADD
will truncate a specified number value that has a decimal fraction. It will not round the number value in this situation.
date
An expression that can resolve to one of the following values:
date
datetime
datetimeoffset
datetime2
smalldatetime
time
For date, DATEADD
will accept a column expression, expression, string literal, or user-defined variable. A string literal value must resolve to a datetime. Use four-digit years to avoid ambiguity issues.
Return Types
The return value data type for this method is dynamic. The return type depends on the argument supplied for date
. If the value for date
is a string literal date, DATEADD
returns a datetime value. If another valid input data type is supplied for date
, DATEADD
returns the same data type. DATEADD
raises an error if the string literal seconds scale exceeds three decimal place positions (.nnn) or if the string literal contains the time zone offset part.
Example 1
Incrementing datepart by an interval of 1
Example 2
Incrementing more than one level of datepart in one statement
Each of these statements increments datepart by a number large enough to additionally increment the next higher datepart of date:
Example 3
Using expressions as arguments for the number and date parameters
Specifying a column as date
This example adds 2
(two) days to each value in the OrderDate
column, to derive a new column named PromisedShipDate
:
EOMONTH
The EOMONTH function returns the last day of the month containing a specified date, with an optional offset.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
Syntax
Arguments
start_date
A date expression that specifies the date for which to return the last day of the month.
month_to_add
An optional integer expression that specifies the number of months to add to start_date.
If the month_to_add argument has a value, then EOMONTH
adds the specified number of months to start_date, and then returns the last day of the month for the resulting date. If this addition overflows the valid range of dates, then EOMONTH
will raise an error.
Return Types
Remarks
The EOMONTH
function can remote to SQL Server 2012 (11.x) servers and higher. It cannot be remote to servers with a version lower than SQL Server 2012 (11.x).
Example 1
EOMONTH with explicit datetime type
Example 2
EOMONTH with string parameter and implicit conversion
Example 3
EOMONTH with and without the month_to_add parameter
SWITCHOFFSET
The SWITCHOFFSEET function returns a datetimeoffset value that is changed from the stored time zone offset to a specified new time zone offset.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
Syntax
Arguments
DATETIMEOFFSET
DATETIMEOFFSET
Is an expression that can be resolved to a datetimeoffset(n) value.
time_zone
Is a character string in the format [+|-]TZH:TZM or a signed integer (of minutes) that represents the time zone offset, and is assumed to be daylight-saving aware and adjusted.
Return Types
datetimeoffset with the fractional precision of the DATETIMEOFFSET argument.
Remarks
Use SWITCHOFFSET to select a datetimeoffset value into a time zone offset that is different from the time zone offset that was originally stored. SWITCHOFFSET does not update the stored time_zone value.
SWITCHOFFSET can be used to update a datetimeoffset column.
Using SWITCHOFFSET with the function GETDATE() can cause the query to run slowly. This is because the query optimizer is unable to obtain accurate cardinality estimates for the datetime value. To resolve this problem, use the OPTION (RECOMPILE) query hint to force the query optimizer to recompile a query plan the next time the same query is executed. The optimizer will then have accurate cardinality estimates and will produce a more efficient query plan.
Example
TODATETIMEOFFSET
TODATETIMEOFFSET function returns a datetimeoffset value that is translated from a datetime2 expression.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
Syntax
Arguments
expression
Is an expression that resolves to a datetime2 value.
time_zone
Is an expression that represents the time zone offset in minutes (if an integer), for example -120, or hours and minutes (if a string), for example '+13:00'. The range is +14 to -14 (in hours). The expression is interpreted in local time for the specified time_zone.
Return Types
datetimeoffset. The fractional precision is the same as the datetime argument.
Example 1
Changing the time zone offset of the current date and time
The following example changes the zone offset of the current date and time to time zone -07:00
.
Example 2
Changing the time zone offset in minutes
The following example changes the current time zone to -120
minutes.
Example 3
Adding a 13-hour time zone offset
The following example adds a 13-hour time zone offset to a date and time.
Last updated