GETDATE() Function in SQL Server
Last Updated :
18 Jan, 2021
GETDATE() function :
This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm:ss.mmm’ pattern.
Features :
- This function is used to find the present date and time of the database system.
- This function comes under Date Functions.
- This function doesn’t accept any parameter.
- This function returns output in ‘YYYY-MM-DD hh:mm:ss.mmm‘ format.
Syntax :
GETDATE()
Parameter :
This method doesn’t accept any parameter.
Returns :
It returns the present date and time of the database system in a ‘YYYY-MM-DD hh:mm:ss.mmm‘ format.
Example-1 :
Using GETDATE() function and getting the output.
SELECT GETDATE();
Output :
2021-01-03 14:42:58.970
Here, the output will vary each time the code is compiled as this method returns the current date and time.
Example-2 :
Using GETDATE() as a default value in the below example and getting the output.
CREATE TABLE get_date
(
id_num INT IDENTITY,
message VARCHAR(150) NOT NULL,
generated_at DATETIME NOT NULL
DEFAULT GETDATE(),
PRIMARY KEY(id_num)
);
Inserting Data :
INSERT INTO get_date(message)
VALUES('Its the first message.');
INSERT INTO get_date(message)
VALUES('get_date');
Read Data :
SELECT id_num, message, generated_at
FROM get_date;
Output :
Index No. |
id_num |
message |
generated_at |
1 |
1 |
Its the first message. |
03.01.2021 15:49:48 |
2 |
2 |
get_date |
03.01.2021 15:49:48 |
Here, firstly you need to create a table then insert values into it then generate the required output using GETDATE() 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 return the current date and time of the database system.
Share your thoughts in the comments
Please Login to comment...