SYSDATETIME() Function in SQL Server
Last Updated :
21 Jan, 2021
SYSDATETIME() :
This function in SQL Server is used to return the computer's date and time in which the SQL Server is operating at present.
Features :
- This function is used to find the computer's date and time in which the SQL Server is operating.
- This function comes under Date Functions.
- This function doesn't accept any parameter.
- This function returns the output in 'YYYY-MM-DD hh:mm:ss.mmm' format.
Syntax :
SYSDATETIME()
Parameter :
This method doesn't accept any parameter.
Returns :
It returns the computer's date and time in which the SQL Server is operating in a 'YYYY-MM-DD hh:mm:ss.mmm' format.
Example-1 :
Using SYSDATETIME() function and getting the output.
SELECT SYSDATETIME();
Output :
2021-01-03 17:49:28.0575187
Here, the output will vary each time the code is compiled as this method returns the current date and time.
Example-2 :
Using SYSDATETIME() as a default value in the below example and getting the output.
CREATE TABLE system_date_time
(
id_num INT IDENTITY,
message VARCHAR(150) NOT NULL,
generated_at DATETIME NOT NULL
DEFAULT SYSDATETIME(),
PRIMARY KEY(id_num)
);
INSERT INTO system_date_time(message)
VALUES('Its the first message.');
INSERT INTO system_date_time(message)
VALUES('system_date_time');
SELECT
id_num,
message,
generated_at
FROM
system_date_time;
Output :
|id_num | message | generated_at
-------------------------------------------------------------
1 | 1 | Its the first message.| 03.01.2021 18:53:56
-------------------------------------------------------------
2 | 2 | system_date_time | 03.01.2021 18:53:56
Here, firstly you need to create a table then insert values into it then generate the required output using the SYSDATETIME() function as a default value.
Note: For running the above code use SQL server compiler, you can also use an online compiler.
Example-3 :
Using CONVERT() function in order to translate the output of the SYSDATETIME() function into the current date only.
SELECT CONVERT(DATE, SYSDATETIME());
Output :
2021-01-07
Here, the output may vary every time you run the code as it returns the current date.
Example-4 :
Using CONVERT() function in order to translate the output of the SYSDATETIME() function into the current time only.
SELECT CONVERT(TIME, SYSDATETIME());
Output :
06:20:12.2400986
Here, the output may vary every time you run the code as it returns the current time.
Application :
This function is used to return the current date and time of the computer in which the SQL server is operating.
Similar Reads
YEAR() Function in SQL Server
The YEAR() function in SQL Server is a powerful tool designed to extract the year component from a given date or datetime expression. It allows users to isolate the year as an integer value and facilitating various date-related operations and analyses.In this article, We will learn about the YEAR()
2 min read
SQRT() Function in SQL Server
SQRT() function : This function in SQL Server is used to return the square root of a specified positive number. For example, if the specified number is 81, this function will return 9. Features : This function is used to find the square root of a given number. This function accepts only positive num
2 min read
SYSDATE() function in MySQL
SYSDATE() function in MySQL is used to return the current date and time in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS.uuuuuu format depending on the context of the function. Syntax : SYSDATE() Parameter : This method does not accept any parameter. Returns : It returns the current date and time value. Exa
1 min read
CURRENT_TIMESTAMP() Function in SQL Server
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-DD hh:mm:ss.mmm format.In this article, We will learn about CURRENT_TIMESTAMP()
3 min read
DAY() Function in SQL Server
DAY() function : This function in SQL Server is used to return the day of the month i.e, from 1st to 31st for date stated. Features : This function is used to find the day of the month for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, date.
2 min read
SIGN() Function in SQL Server
In SQL Server, the SIGN() function is a mathematical function used to determine the sign of a given numeric expression. This function is particularly useful when we want to evaluate whether a number is positive, negative or zero. In this article, We will learn about SIGN() Function in SQL Server in
3 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read
SUBDATE() function in MySQL
SUBDATE() function in MySQL is used to subtracts a time value (as interval) from a given date. Syntax : SUBDATE(date, INTERVAL expr unit) Parameter : This function accepts three parameters as given below : date : First specified date. expr : The value of the time/date interval to subtract. unit : Th
2 min read
MONTH() Function in SQL Server
MONTH() function : This function in SQL Server is used to return the month of the year i.e, from 1 to 12 for a date stated. Features : This function is used to find the month of the year for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, dat
2 min read
LTRIM() Function in SQL Server
The LTRIM() function in SQL Server removes all the space characters found on the left-hand side of the string. It removes the leading spaces from a string, SyntaxThe LTRIM function for SQL Server syntax is: LTRIM(string, [trim_string]) Parameter: string - The string from which the leading space char
2 min read