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





download














get free sql tips
agree to terms