SQL Server CURRENT_TIMESTAMP Function


By:

The CURRENT_TIMESTAMP function returns the current server time stamp using the datetime format. This function is an ANSI SQL standard and it returns the same value the GETDATE() function returns. It is recommended to use this function instead of GETDATE(), because this function is an ANSI standard while GETDATE() is a SQL Server specific function.

Syntax

CURENT_TIMESTAMP

Parameters

  • No parameters required

Simple CURRENT_TIMESTAMP Example

The following is a simple CURRENT_TIMESTAMP example.

SELECT CURRENT_TIMESTAMP as timestamp
simple CURRENT_TIMESTAMP example

Difference between GETDATE() and CURRENT_TIMESTAMP

The following example shows that there are differences between the GETDATE() function and CURRENT_TIMESATAMP.

SELECT DATEDIFF(SECOND, GETDATE(), CURRENT_TIMESTAMP) as difference
difference between current_timestamp and getdate

How to set the CURRENT_TIMESTAMP with a Custom Format

The following example will show the CURRENT_TIMESTAMP in the dd-MM-yyyy hh:mm:ss tt format.

SELECT FORMAT(CURRENT_TIMESTAMP, 'dd-MM-yyyy hh:mm:ss tt') as formatdate
current_timestamp in a custom format

How to set the CURRENT_TIMESTAMP with a Different Cultural Format

The following example will set the CURRENT_TIMESTAMP in the Japanese date format.

SELECT FORMAT (CURRENT_TIMESTAMP, 'dddd dd, MMMM, yyyy', 'ja-jp') as japanese
current_timestamp in japanese

Using CURRENT_TIMESTAMP with Data from a Table

The following example will show ProductIDs with a StartDate between 8 and 9 years ago using CURRENT_TIMESTAMP as the current time.

SELECT Productid, DATEDIFF(YEAR, StartDate, CURRENT_TIMESTAMP) as years
FROM Production.ProductCostHistory
WHERE DATEDIFF(YEAR, StartDate, CURRENT_TIMESTAMP) BETWEEN 8 AND 9
DATEDIFF with CURRENT_TIMESTAMP

Related Articles


Last Update: 1/4/2022




Comments For This Article




Wednesday, April 10, 2024 - 1:12:08 PM - Jeremy Kadlec Back To Top (92168)
Jeff,

Thank you for the feedback. We will get this on the list to get updated.

Thank you,
Jeremy Kadlec
Community Co-Leader

Tuesday, April 9, 2024 - 9:29:41 AM - Jeff Moden Back To Top (92161)
Nice article but teaching how to use FORMAT without teaching how to do the equivalent CONVERT is a serious oversight, IMHO, because of the fact that FORMAT is at least 17 times slower and up to 27 times slower than CONVERT.

Yep... I know CONVERT is taught later on but, IMHO, MUST at least be demonstrated when FORMAT is demonstrated for the reason I stated.














get free sql tips
agree to terms