Summary: in this tutorial, you will learn how to use the SQL Server CURRENT_TIMESTAMP
function to get the current database system timestamp as a DATETIME
value.
SQL Server CURRENT_TIMESTAMP Overview
The CURRENT_TIMESTAMP
function returns the current timestamp of the operating system of the server on which the SQL Server Database runs. The returned timestamp is a DATETIME
value without the time zone offset.
The CURRENT_TIMESTAMP
function takes no argument:
CURRENT_TIMESTAMP
Code language: SQL (Structured Query Language) (sql)
The CURRENT_TIMESTAMP
is the ANSI SQL equivalent to GETDATE()
.
You can use the CURRENT_TIMESTAMP
function anywhere a DATETIME
expression is accepted.
SQL Server CURRENT_TIMESTAMP function examples
Let’s take some example of using the CURRENT_TIMESTAMP
function.
A ) Simple CURRENT_TIMESTAMP example
The following example uses the CURRENT_TIMESTAMP
function to return the current date and time:
SELECT
CURRENT_TIMESTAMP AS current_date_time;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
current_date_time
-----------------------
2019-02-23 20:02:21.550
(1 row affected)
Code language: SQL (Structured Query Language) (sql)
B) Using CURRENT_TIMESTAMP function as a default value for table columns example
First, create a new table named current_timestamp_demos
whose created_at
column accepts a default value as the timestamp at which a row is inserted:
CREATE TABLE current_timestamp_demos
(
id INT IDENTITY,
msg VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL
DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id)
);
Code language: SQL (Structured Query Language) (sql)
Second, insert two rows into the table:
INSERT INTO current_timestamp_demos(msg)
VALUES('This is the first message.');
INSERT INTO current_timestamp_demos(msg)
VALUES('current_timestamp demo');
Code language: SQL (Structured Query Language) (sql)
Third, query data from the current_timestamp_demos
table:
SELECT
id,
msg,
created_at
FROM
current_timestamp_demos;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
As you can see clearly from the output, the values in the created_at
column took the timestamp returned by the CURRENT_TIMESTAMP
function.
In this tutorial, you have learned how to use the SQL Server CURRENT_TIMESTAMP
function to return the current database system timestamp as a DATETIME
value.