patternsqlMinor
What is the CURRENT_DATE or current date value function for SQL Server?
Viewed 0 times
current_datethewhatsqlfunctiondatevalueforcurrentserver
Problem
What is the SQL Server functionality that allows you to get the current date as a
Using PostgreSQL, I'm looking for something like,
DATE type like the SQL Standard's CURRENT_DATE feature. What is the `` for SQL Server?Using PostgreSQL, I'm looking for something like,
SELECT CURRENT_DATE;
current_date
--------------
2018-06-27
(1 row)Solution
You can use either
Example:
Results:
fn_GetDate fn_SysDateTime
2018-06-27 10:31:18.963 2018-06-27 10:31:18.9659170
See Date and Time Data Types and Functions (Transact-SQL) in the product documentation.
For completeness, SQL Server also recognises
This returns
The built-in functions are recommended over ODBC scalar functions.
GETDATE (return type datetime) or SYSDATETIME (return type datetime2), with the difference being the precision up to nanoseconds for SYSDATETIME().Example:
SELECT GETDATE() fn_GetDate, SYSDATETIME() fn_SysDateTimeResults:
fn_GetDate fn_SysDateTime
2018-06-27 10:31:18.963 2018-06-27 10:31:18.9659170
See Date and Time Data Types and Functions (Transact-SQL) in the product documentation.
For completeness, SQL Server also recognises
CURRENT_DATE as mentioned in the question, as an ODBC scalar function:SELECT {fn CURRENT_DATE()};This returns
varchar(10), so would need an explicit cast or convert to the date data type:SELECT CONVERT(date, {fn CURRENT_DATE()});The built-in functions are recommended over ODBC scalar functions.
Code Snippets
SELECT GETDATE() fn_GetDate, SYSDATETIME() fn_SysDateTimeSELECT {fn CURRENT_DATE()};SELECT CONVERT(date, {fn CURRENT_DATE()});Context
StackExchange Database Administrators Q#210683, answer score: 9
Revisions (0)
No revisions yet.