Date and time functions for T-SQL
This topic provides reference information about date and time functions in PostgreSQL compared to Microsoft SQL Server, which is valuable for database administrators and developers migrating from SQL Server to Amazon Aurora PostgreSQL. You can understand the differences in function names, syntax, and behavior between the two database systems when working with temporal data. The topic highlights key date and time functions, their equivalents across platforms, and offers guidance on handling potential compatibility issues.
Feature compatibility | AWS SCT / AWS DMS automation level | AWS SCT action code index | Key differences |
---|---|---|---|
|
|
PostgreSQL is using different function names. |
SQL Server Usage
Date and Time Functions are scalar functions that perform operations on temporal or numeric input and return temporal or numeric values.
System date and time values are derived from the operating system of the server on which SQL Server is running.
This section doesn’t address time zone considerations and time zone aware functions. For more information about time zone handling, see Data Types.
Syntax and Examples
The following table includes the most commonly used date and time functions.
Function | Purpose | Example | Result | Comments |
---|---|---|---|---|
|
Return a datetime value that contains the current local or UTC date and time. |
|
2018-04-05 15:53:01.380 |
|
|
Return an integer value representing the specified |
|
4, 2018 |
|
|
Returns an integer value of |
|
25 |
How many days left until end of the month. |
|
Returns a datetime value that is calculated with an offset interval to the specified |
|
2018-04-30 15:55:52.147 |
|
|
Converts datetime values to and from string literals and to and from other datetime formats. |
|
2018-04-05 20180405 |
Default date format. Style 112 (ISO) with no separtors. |
For more information, see Date and Time functions
PostgreSQL Usage
Amazon Aurora PostgreSQL-Compatible Edition (Aurora PostgreSQL) provides a very rich set of scalar date and time functions; more than SQL Server.
While some of the functions appear to be similar to those in SQL Server, the functionality is significantly different. Take extra care when migrating temporal logic to Aurora PostgreSQL paradigms.
Functions and Definition
PostgreSQL function | Function definition |
---|---|
|
Subtract from |
|
Current date and time. |
|
Current date. |
|
Current time of day. |
|
Current date and time (start of current transaction). |
|
Get subfield (equivalent to extract). |
|
Truncate to specified precision. |
|
Get subfield. |
|
Test for finite interval. |
|
Adjust interval so 30-day time periods are represented as months. |
|
Adjust interval so 24-hour time periods are represented as days. |
|
Adjust interval using |
|
Current time of day. |
|
Create date from year, month and day fields. |
|
Create interval from years, months, weeks, days, hours, minutes and seconds fields. |
|
Create time from hour, minute and seconds fields. |
|
Create timestamp from year, month, day, hour, minute, and seconds fields. |
|
Create timestamp with time zone from year, month, day, hour, minute, and seconds fields. If the time zone isn’t specified, the current time zone is used. |
NOW |
Current date and time. |
|
Current date and time. |
|
Current date and time (like clock_timestamp, but as a text string). |
|
Current date and time. |
|
Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp. |
Summary
SQL Server function | Aurora PostgreSQL function |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For more information, see Date/Time Functions and Operators