Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The return date and time difference value functions covered in this section are:
DATDIFF function returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.
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.
datepart
The units in which DATEDIFF reports the difference between the startdate and enddate. Commonly used datepart units include month
or second
.
The datepart value cannot be specified in a variable, nor as a quoted string like 'month'
.
The following table lists all the valid datepart values. DATEDIFF accepts either the full name of the datepart, or any listed abbreviation of the full name.
startdate
An expression that can resolve to one of the following values:
date
datetime
datetimeoffset
smalldatetime
time
Use four-digit years to avoid ambiguity.
Use DATEDIFF
in the SELECT <list>
, WHERE
, HAVING
, GROUP BY
and ORDER BY
clauses.
DATEDIFF
implicitly casts string literals as a datetime2 type. This means that DATEDIFF
does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.
Specifying SET DATEFIRST
has no effect on DATEDIFF
. DATEDIFF
always uses Sunday as the first day of the week to ensure the function operates in a deterministic way.
DATEDIFF
may overflow with a precision of minute or higher if the difference between enddate and startdate returns a value that is out of range for int.
Finding the number of days between two dates.
Specifying user-defined variables for startdate and enddate
Example 3: Specifying scalar system functions for startdate and enddate
DATEIFF_BIG function returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.
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.
datepart
The part of startdate and enddate that specifies the type of boundary crossed.
This table lists all valid datepart argument names and abbreviations.
startdate
An expression that can resolve to one of the following values:
date
datetime
datetimeoffset
smalldatetime
time
For date, DATEDIFF_BIG
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. DATEDIFF_BIG
subtracts startdate from enddate. To avoid ambiguity, use four-digit years.
Use DATEDIFF_BIG
in the SELECT <list>
, WHERE
, HAVING
, GROUP BY
and ORDER BY
clauses.
DATEDIFF_BIG
implicitly casts string literals as a datetime2 type. This means that DATEDIFF_BIG
does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.
Specifying SET DATEFIRST
has no effect on DATEDIFF_BIG
. DATEDIFF_BIG
always uses Sunday as the first day of the week to ensure the function operates in a deterministic way.
DATEDIFF_BIG
may overflow with a precision of nanosecond if the difference between enddate and startdate returns a value that is out of range for bigint.
This example uses different types of expressions as arguments for the startdate and enddate parameters. It calculates the number of day boundaries crossed between dates in two columns of a table.
datepart
year
quarter
month
dayofyear
day
week
hour
minute
second
millisecond
microsecond
nanosecond
datepart name
year
quarter
month
dayofyear
day
week
hour
minute
second
millisecond
microsecond
nanosecond
SQL derives all system date and time values from the operating system of the computer on which the instance of SQL Server runs.
SQL Server 2019 (15.x) derives the date and time values through use of the GetSystemTimeAsFileTime() Windows API. The accuracy depends on the computer hardware and version of Windows on which the instance of SQL Server running. This API has a precision fixed at 100 nanoseconds. Use the GetSystemTimeAdjustment() Windows API to determine the accuracy.The return system date and time value functions covered in this section are:
SYSDATETIME returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running.
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.
SQL statements can refer to SYSDATETIME anywhere they can refer to a datetime2(7) expression.
SYSDATETIME is a nondeterministic function. Views and expressions that reference this function in a column cannot be indexed.
Example 1: Getting the Current System Date and Time
Example 2: Getting the Current System Date
Example 3: Getting the Current System Time
SYSDATETIMEOFFSET returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included.
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.
SQL statements can refer to SYSDATETIMEOFFSET anywhere they can refer to a datetimeoffset expression.
SYSDATETIMEOFFSET is a nondeterministic function. Views and expressions that reference this function in a column cannot be indexed.
Example 1: Showing Formats Returned by the Date and Time Functions
Example 2: Converting Date and Time to Date
Example 3: Converting Date and Time to Times
SYSUTCDATETIME returns a datetime2 value that contains the date and time of the computer on which the instance of SQL Server is running. The date and time are returned as UTC time (Coordinated Universal Time). The fractional second precision specification has a range from 1 to 7 digits. The default precision is 7 digits.
SQL statements can refer to SYSUTCDATETIME anywhere they can refer to a datetime2 expression.
SYSUTCDATETIME is a nondeterministic function. Views and expressions that reference this function in a column cannot be indexed.
Example 1: Showing Formats Returned by Date and Time functions
Example 2: Converting Date and Time to Date
Example 3: Converting Date and Time to Time
The following return date and time values from their parts functions covered in this section are:
This function returns a date value that maps to the specified year, month, and day values.
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.
year
An integer expression that specifies a year.
month
An integer expression that specifies a month, from 1 to 12.
day
An integer expression that specifies a day.
DATEFROMPARTS
returns a date value, with the date portion set to the specified year, month and day, and the time portion set to the default. For invalid arguments, DATEFROMPARTS
will raise an error. DATEFROMPARTS
returns null if at least one required argument has a null value.
DATETIME2FROMPARTS function returns a datetime2 value for the specified date and time arguments. The returned value has a precision specified by the precision argument.
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.
year
An integer expression that specifies a year.
month
An integer expression that specifies a month.
day
An integer expression that specifies a day.
hour
An integer expression that specifies the hours.
minute
An integer expression that specifies the minutes.
seconds
An integer expression that specifies the seconds.
fractions
An integer expression that specifies a fractional seconds value.
percision
An integer expression that specifies the precision of the datetime2 value that DATETIME2FROMPARTS
will return.
DATETIME2FROMPARTS
returns a fully initialized datetime2 value. DATETIME2FROMPARTS
will raise an error if at least one required argument has an invalid value. DATETIME2FROMPARTS
returns null if at least one required argument has a null value. However, if the precision argument has a null value, DATETIME2FROMPARTS
will raise an error.
The fractions argument depends on the precision argument. For example, for a precision value of 7, each fraction represents 100 nanoseconds; for a precision of 3, each fraction represents a millisecond. For a precision value of zero, the value of fractions must also be zero; otherwise, DATETIME2FROMPARTS
will raise an error.
Example 1: Without Fractions of a Second
Example 2: With Fractions of a Second
When fractions have a value of 5, and precision has a value of 1, the value of fractions represents 5/10 of a second.
When fractions have a value of 50, and precision has a value of 2, the value of fractions represents 50/100 of a second.
When fractions have a value of 500, and precision has a value of 3, then the value of fractions represents 500/1000 of a second.
DATETIMEFROMPARTS function returns a datetime value for the specified date and time arguments.
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.
year
An integer expression that specifies a year.
month
An integer expression that specifies a month.
day
An integer expression that specifies a day.
hour
An integer expression that specifies hours.
minute
An integer expression that specifies minutes.
seconds
An integer expression that specifies seconds.
milliseconds
An integer expression that specifies milliseconds.
DATETIMEFROMPARTS
returns a fully initialized datetime value. DATETIMEFROMPARTS
will raise an error if at least one required argument has an invalid value. DATETIMEFROMPARTS
returns null if at least one required argument has a null value.
Returns a datetimeoffset value for the specified date and time arguments. The returned value has a precision specified by the precision argument and an offset as specified by the offset arguments.
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.
year
An integer expression that specifies a year.
month
An integer expression that specifies a month.
day
An integer expression that specifies a day.
hour
An integer expression that specifies hours.
minute
An integer expression that specifies minutes.
seconds
An integer expression that specifies seconds.
frcations
An integer expression that specifies a fractional seconds value.
hour_offset
An integer expression that specifies the hour portion of the time zone offset.
minute_offset
An integer expression that specifies the minute portion of the time zone offset.
precision
An integer literal value that specifies the precision of the datetimeoffset value that DATETIMEOFFSETFROMPARTS
will return.
DATETIMEOFFSETFROMPARTS
returns a fully initialized datetimeoffset data type. The offset arguments represent the time zone offset. For omitted offset arguments, DATETIMEOFFSETFROMPARTS
assumes a time zone offset of 00:00
- in other words, no time zone offset. For specified offset arguments, DATETIMEOFFSETFROMPARTS
expects values for both arguments, and both values positive or negative. If minute_offset has a value and hour_offset has no value, DATETIMEOFFSETFROMPARTS
will raise an error. DATETIMEOFFSETFROMPARTS
will raise an error if the other arguments have invalid values. If at least one required arguments have a NULL
value, then DATETIMEOFFSETFROMPARTS
will return NULL
. However, if the precision argument has a NULL
value, then DATETIMEOFFSETFROMPARTS
will raise an error.
The fractions argument depends on the precision argument. For example, for a precision value of 7, each fraction represents 100 nanoseconds; for a precision of 3, each fraction represents a millisecond. For a precision value of zero, the value of fractions must also be zero; otherwise, DATETIMEOFFSETFROMPARTS
will raise an error.
Example 1: Without Fractions of a Second
Example 2: With Fractions of a Second
This example shows the use of the fractions and precision parameters:
When fractions have a value of 5, and precision has a value of 1, the value of fractions represents 5/10 of a second.
When fractions have a value of 50, and precision has a value of 2, the value of fractions represents 50/100 of a second.
When fractions have a value of 500, and precision has a value of 3, then the value of fractions represents 500/1000 of a second.
SMALLDATETIMEFROMPARTS returns a smalldatetime value for the specified date and time.
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.
year
Integer expression specifying a year.
month
Integer expression specifying a month.
day
Integer expression specifying a day.
hour
Integer expression specifying hours.
minute
Integer expression specifying minutes.
This function acts as a constructor for a fully initialized smalldatetime value. If the arguments are not valid, then an error is thrown. If required arguments are null, then null is returned.
TIMEFROMPARTS returns a time value for the specified time and with the specified precision.
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.
hour
Integer expression specifying hours.
minute
Integer expression specifying minutes.
seconds
Integer expression specifying seconds.
fractions
Integer expression specifying fractions.
precision
Integer literal specifying the precision of the time value to be returned.
TIMEROMPARTS returns a fully initialized time value. If the arguments are invalid, then an error is raised. If any of the parameters are null, null is returned. However, if the precision argument is null, then an error is raised.
The fractions argument depends on the precision argument. For example, if precision is 7, then each fraction represents 100 nanoseconds; if precision is 3, then each fraction represents a millisecond. If the value of precision is zero, then the value of fractions must also be zero; otherwise, an error is raised.
Example 1: Without Fractions of a Second
Example 2: With Fractions of a Second
The following example demonstrates the use of the fractions and precision parameters:
When fractions have a value of 5 and precision has a value of 1, then the value of fractions represents 5/10 of a second.
When fractions have a value of 50 and precision has a value of 2, then the value of fractions represents 50/100 of a second.
When fractions have a value of 500 and precision has a value of 3, then the value of fractions represents 500/1000 of a second.
The validate date and time value function covered in this section is:
ISDATE checks an expression to see if it is correct.
It will return 1 if the expression is a valid date, time, or datetime value; otherwise, it will return 0. ISDATE will also return 0 if the expression is a datetime2 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.
expression
Is a character string or expression that can be converted to a character string. The expression must be less than 4,000 characters. Date and time data types, except datetime and smalldatetime, are not allowed as the argument for ISDATE.
ISDATE is deterministic only when used with the CONVERT function, if the CONVERT style parameter is specified, and style is not equal to 0, 100, 9, or 109.
The return value of ISDATE depends on the settings set by SET DATEFORMAT, SET LANGUAGE and Configure the default language Server Configuration Option.
Using ISDATE to Test Valid datetime Expression
The modify date and time value functions covered in this section are:
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.
datepart
The part of date to which DATEADD
adds an integer number. This table lists all valid datepart arguments.
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.
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.
Incrementing datepart by an interval of 1
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:
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
:
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.
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.
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).
EOMONTH with explicit datetime type
EOMONTH with string parameter and implicit conversion
EOMONTH with and without the month_to_add parameter
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.
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.
datetimeoffset with the fractional precision of the DATETIMEOFFSET argument.
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.
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.
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.
datetimeoffset. The fractional precision is the same as the datetime argument.
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
.
Changing the time zone offset in minutes
The following example changes the current time zone to -120
minutes.
Adding a 13-hour time zone offset
The following example adds a 13-hour time zone offset to a date and time.
The return date and time part functions covered in this section are:
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.
The DATENAME function returns a character string representing the specified datepart of the specified date.
datepart
The specific part of the date argument that DATENAME
will return. This table lists all valid datepart arguments.
date
An expression that can resolve to one of the following data types:
date
datetime
datetimeoffset
datetime2
smalldatetime
time
For date, DATENAME
will accept a column expression, expression, string literal, or user-defined variable. Use four-digit years to avoid ambiguity issues.
Use DATENAME
in the following clauses:
GROUP BY
HAVING
ORDER BY
SELECT <list>
WHERE
SELECT DATENAME(datepart,'2007-10-30 12:15:32.1234567 +05:10');
Result Set
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.
DATEPART function returns an integer representing the specified datepart of the specified date.
datepart
The specific part of the date argument for which DATEPART
will return an integer. This table lists all valid datepart arguments.
date
An expression that resolves to one of the following data types:
date
datetime
datetimeoffset
datetime2
smalldatetime
time
For date, DATEPART
will accept a column expression, expression, string literal, or user-defined variable. Use four-digit years to avoid ambiguity issues.
DATEPART
can be used in the select list, WHERE, HAVING, GROUP BY, and ORDER BY clauses.
DATEPART implicitly casts string literals as a datetime2 type in SQL Server 2019 (15.x). This means that DATENAME does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.
Example 1: Returns Base Year
Example 2: Returns Day the Day Part of the Date
Example 3: Returns the year Part of the Date
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.
DAY function returns an integer that represents the day (day of the month) of the specified date.
date
An expression that resolves to one of the following data types:
date
datetime
datetimeoffset
datetime2
smalldatetime
time
For date, DAY
will accept a column expression, expression, string literal, or user-defined variable.
Example 1:
This returns 30
- the number of the day itself
Example 2:
This statement returns 1900, 1, 1
. The date argument has a number value of 0
. SQL Server interprets 0
as January 1, 1900.
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.
MONTH returns an integer that represents the month of the specified date.
date
Is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value. The date argument can be an expression, column expression, user-defined variable, or string literal.
Example 1:
The following statement returns 4
. This is the number of the month.
Example 2:
The following statement returns 1900, 1, 1
. The argument for date is the number 0
. SQL Server interprets 0
as January 1, 1900.
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.
YEAR function returns an integer that represents the year of the specified date.
date
Is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value. The date argument can be an expression, column expression, user-defined variable or string literal.
Example 1:
The following statement returns 2020
. This is the number of the year.
Example 2:
The following statement returns 1900, 1, 1
. The argument for date is the number 0
. SQL Server interprets 0
as January 1, 1900.
Perform operations on a date and time input values and return string, numeric, or date and time values.
The date and time date type and functions covered in this section are:
datepart
year
quarter
month
dayofyear
day
week
weekday
hour
minute
second
millisecond
microsecond
nanosecond
Data type
Format
Range
Accuracy
Storage size (bytes)
User-defined fractional second precision
Time zone offset
time
hh:mm:ss[.nnnnnnn]
00:00:00.0000000 through 23:59:59.9999999
100 nanoseconds
3 to 5
Yes
No
date
YYYY-MM-DD
0001-01-01 through 9999-12-31
1 day
3
No
No
smalldatetime
YYYY-MM-DD hh:mm:ss
1900-01-01 through 2079-06-06
1 minute
4
No
No
datetime
YYYY-MM-DD hh:mm:ss[.nnn]
1753-01-01 through 9999-12-31
0.00333 second
8
No
No
datetime2
YYYY-MM-DD hh:mm:ss[.nnnnnnn]
0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999
100 nanoseconds
6 to 8
Yes
No
datetimeoffset
YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm
0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999 (in UTC)
100 nanoseconds
8 to 10
Yes
datepart
year
quarter
month
dayofyear
day
week
weekday
hour
minute
second
millisecond
microsecond
nanosecond
TZoffset
ISO_WEEK
datepart
Return value
year
2007
quarter
4
month
October
dayofyear
303
day
30
week
44
weekday
Tuesday
hour
12
minute
15
second
32
millisecond
123
microsecond
123456
nanosecond
123456700
TZoffset
+05:10
ISO_WEEK
44