![]() |
VOOZH | about |
In SQL Server, the CURRENT_TIMESTAMP function is a widely used feature for retrieving the current date and time directly from the database server. It returns the exact moment when the SQL query is executed in the YYYY-MM-DDhh:mm:ss.mmm format.
In this article, We will learn about CURRENT_TIMESTAMP() Function in SQL Server by understanding various examples.
CURRENT_TIMESTAMP() function in SQL is used to retrieve the current date and time from the database server. CURRENT_TIMESTAMP in SQL ServerYYYY-MM-DD HH:MM:SS format.TIMESTAMP or DATETIME data type, depending on the database system.Syntax:
CURRENT_TIMESTAMPParameter:
This method doesn't accept any parameter.
Returns:
It returns the current date as well as time and the format of the output returned is 'YYYY-MM-DD hh:mm:ss.mmm'.
Let's Retrieve the current date and time from the SQL Server in order to track or display the exact timestamp when a query is executed.
SELECT CURRENT_TIMESTAMP AS current_date_and_time;Output:
| current_date_and_time |
|---|
| 2024-09-13 15:32:24.123 |
Explanation:
The query `SELECT CURRENT_TIMESTAMP AS current_date_and_time;` retrieves the current date and time from the SQL Server at the moment the query is executed and displays it under the alias `current_date_and_time`.
The result is returned in the `YYYY-MM-DD hh:mm:ss.mmm` format.
Let's Create a table that automatically tracks the timestamp when a new record is inserted and allowing for the logging of messages along with the precise date and time of their creation without manual input of the timestamp.
CREATE TABLE current_time_stamp (
id_num INT IDENTITY,
message VARCHAR(150) NOT NULL,
generated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id_num)
);
-- Insert some data
INSERT INTO current_time_stamp (message)
VALUES ('This is the first message.');
INSERT INTO current_time_stamp (message)
VALUES ('This is the second message.');
-- Read data from the table
SELECT
id_num,
message,
generated_at
FROM
current_time_stamp;
Output:
| Index no. | id_num | message | generated_at |
|---|---|---|---|
| 1 | 1 | Its the first message. | 31.12.2020 15:57:01 |
| 2 | 2 | current_time_stamp | 31.12.2020 15:57:01 |
Explanation:
Note: For running the above code use SQL server compiler, you can also use an online compiler.
The CURRENT_TIMESTAMP function in SQL Server offers a simple and effective method for capturing the current date and time. Whether you are logging events or tracking changes, this function provides precise timestamping without the need for additional parameters.
Can I pass any parameters to the CURRENT_TIMESTAMP function?
No, the
CURRENT_TIMESTAMPfunction does not accept any parameters. It simply returns the current date and time based on the server’s clock.
What is the data type returned by the CURRENT_TIMESTAMP function?
The
CURRENT_TIMESTAMPfunction typically returns a value of theDATETIMEdata type, which includes both the date and the time up to milliseconds.
How does CURRENT_TIMESTAMP differ from GETDATE() in SQL Server?
Both
CURRENT_TIMESTAMPandGETDATE()return the current date and time in SQL Server. However,GETDATE()is a SQL Server-specific function, whereasCURRENT_TIMESTAMPis ANSI SQL compliant and can be used across multiple database systems.