CURRENT_TIMESTAMP() Function in SQL Server
CURRENT_TIMESTAMP() function :
This function in SQL Server is used to return the current date as well as time. And the format of the output is as follows.
'YYYY-MM-DD hh:mm:ss.mmm'
Features :
- This function is used to find the current date and time.
- This function comes under Date Functions.
- This function doesn’t accept any parameter.
- This function can also be used as a default value in some codes.
Syntax :
CURRENT_TIMESTAMP
Parameter :
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’.
Example-1 :
Using CURRENT_TIMESTAMP function and getting the current date as well as time.
SELECT CURRENT_TIMESTAMP AS current_date_and_time;
Output :
current_date_and_time -------------------------- 2020-12-31 12:32:24.100
So, here the output may vary every time you run this code as this function returns the current date and time.
Example-2 :
Using CURRENT_TIMESTAMP as a default value in the below example and getting the output.
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) );
Inserting data into table –
INSERT INTO current_time_stamp(message) VALUES('Its the first message.'); INSERT INTO current_time_stamp(message) VALUES('current_time_stamp');
Reading data from 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 |
Here, firstly you need to create a table then insert values into it then generate the required output using CURRENT_TIMESTAMP function as a default value.
Note –
For running the above code use SQL server compiler, you can also use an online compiler.
Application :
This function is used to find the current date as well as time.
Contact Us