In this blog post, we’ll explore various methods to retrieve the current date using SQL Server commands.
Get Current Date & Time in SQL Server
GETDATE()
:
- Returns the current date and time with accuracy to one-third of a second.
- Precision is up to milliseconds.
-- Get current date and time SELECT GETDATE() AS CurrentDate;
Get Precise Date & Time in SQL Server
SYSDATETIME()
:
- Provides a higher precision level, including fractional seconds.
- Returns the current date and time with accuracy to nanoseconds.
-- Get precision datetimestamp SELECT SYSDATETIME() AS CurrentDateTime;
if you require a higher level of precision in your timestamps, especially in scenarios where extremely accurate time tracking is necessary (such as financial transactions), you may opt for SYSDATETIME()
. However, for most general purposes, GETDATE()
is sufficient and often used due to its simplicity and performance.
Customise Output of DateTime Value
If you’re looking to use the output date time value from above, you may want to customize the format:
-- Get custom formatted date SELECT FORMAT(GETDATE(), 'yyyy-MM-dd') AS FormattedDate;