The Wayback Machine - https://web.archive.org/web/20240928070558/https://www.geeksforgeeks.org/how-to-get-current-date-and-time-in-sql/
Open In App

How to Get Current Date and Time in SQL?

Last Updated : 17 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

There are many built-in functions to get the current date and time in SQL. In this article, we will discuss how to get the current date and time in SQL with examples.

SQL Get Current Date and Time

Three in-built SQL functions to fetch current date and time are:

  • SQL GETDATE() Function
  • SQL CURRENT_TIMESTAMP() Function
  • SQL SYSDATETIME() Function

SQL GETDATE() Function

SQL GETDATE() function returns the current database system date and time in the format ‘YYYY-MM-DD hh:mm: ss. mmm’.

Syntax

The GETDATE() function syntax is:

SELECT GETDATE();

Example

In this example, we use the GetDate() function in SQL to get the current date and time.

Query

SELECT GetDate() AS 'CurrentDATETime';

Output

sql getdate() function example output

Output

Get Only Time Part from GETDATE() Function Example

In this example, we will get only the time part from the GETDATE() function in SQL.

Query

SELECT CONVERT(VARCHAR(8), GETDATE(),108)'hh:mi:ss'

Output

time part from getdate() function

Time Part from GETDATE() Function

SQL CURRENT_TIMESTAMP() Function

SQL CURRENT_TIMESTAMP() function used to find the current TIMESTAMP (current Date and Time). The CURRENT_TIMESTAMP returns the same result as GETDATE(). 

Syntax

The CURRENT_TIMESTAMP() function syntax is:

CURRENT_TIMESTAMP

Example

In this example, we use the CURRENT_TIMESTAMP() function in SQL to get the current date and time.

Query

SELECT CURRENT_TIMESTAMP AS "CURRENTTIMESTAMP"; 

Output

sql current_timestamp() function example output

Output

SQL SYSDATETIME() Function

SQL SYSDATETIME() function is also used to get the current time of the System on which the instance of SQL Server is running. SYSDATETIME() function provides more fractional seconds precision compared to the GETDATE() function.

We can fetch the TIME part from the DATE and TIME value returned from the SYSDATETIME() function as below:

Syntax

The SYSDATETIME() function syntax is:

SYSDATETIME()

Example

In this example, we use the SYSDATETIME() function in SQL to get the current date and time.

Query

SELECT SYSDATETIME() 'Current TIME using SYSDATETIME()'

Output

sql sysdatetime() function example output

Output


Previous Article
Next Article

Similar Reads

How To Get the Current Date Without Time in T-SQL?
To get the current date in T-SQL use the GETDATE() function. The result of the function is DateTime data type meaning, it contains both the date and the time, e.g. 2022-07-10 10:32:04. However, to get the current date without time in T-SQL use the CAST() function. This function takes any expression or any column name as the first argument. Then you
1 min read
How to Write a SQL Query For a Specific Date Range and Date Time?
In SQL, some problems require us to retrieve rows based on their dates and times. For such cases, we use the DATETIME2 datatype present in SQL. For this article, we will be using the Microsoft SQL Server as our database. Note - Here, we will use the WHERE and BETWEEN clauses along with the query to limit our rows to the given time. The pattern of s
2 min read
How To Get Current Date in Power Query?
Power query is a analysis tool in excel.From 2016 version onwards it was inbuilted in the Excel . Power Query helps us to perform all type of tasks regarding to transformation.  In this article is we will see how to get current date in power query . Getting Current Date in Power QueryTo get the current date in Power Query,we use the function "=Date
2 min read
Numeric and Date-time data types in SQL Server
MS SQL Server supports a wide range of data types. There are a few more important data types that are included in the article. In this article, we will cover numeric SQL server data types and different date-time data types. Let’s discuss one by one. bit : A bit is the smallest unit of a computer system. A bit can be either 0 or 1. The bit datatype
3 min read
Date and Time Functions in PL/SQL
Date and Time Functions in PL/SQL are useful for date and time operations like extracting date components, performing date arithmetic, and converting between date formats. Date and Time Functions in PL/SQLThere are many useful Date and Time functions in PL/SQL. A table of such functions along with their description is given below: S.noFunctionDescr
4 min read
SQL Date and Time Functions
In SQL, dates are complicated for newbies, since while working with a database, the format of the data in the table must be matched with the input data to insert. In various scenarios instead of date, datetime (time is also involved with date) is used. For storing a date or a date and time value in a database,MySQL offers the following data types:
3 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the 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 a
2 min read
How to Convert Epoch Time to Date in SQL?
To convert Epoch Time to Date in SQL use the DATEADD() function. DATEADD() function in SQL Server is used to add a time or date interval to a specified date and return the modified date. Some features of the SQL DATEADD() function are: This function is used to sum up a time or a date interval to a date specified.This function comes under Date Funct
2 min read
SQL Query to Get a Financial Year Using a Given Date
Here, we are going to Get a Financial Year Using a Given Date in SQL. In this article, we will be making use of the Microsoft SQL Server as our database. For example, finding the Financial year for the give dates in the table. Here, we will first create a database named “geeks” then we will create a table “department” in that database. After, that
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
List the available Databases for Current User in SQL SERVER
Introduction : One of the pre-needful of Database Performance Health Check is to have access to the database which we're going to tune. As SQL DBAs we can also additionally discover it unexpected that quite sometimes, we ended up in a scenario in which we've got a customer who needs us to assist with their database overall performance however they
2 min read
Load .CSV Data into MySQL and Combine Date and Time
Prerequisite - How To Import Timestamp From a CSV File in MySQL? A CSV (comma-separated values) file is a text file with commas separating information. They are most commonly found in spreadsheets and databases and are now primarily used for datasets in data science and machine learning. They assist businesses in exporting large amounts of data to
4 min read
How to Specify a Date Format on Creating a Table and Fill it in SQL?
Whenever we work with databases, we find that almost every single table contains a Date column. After all, the date of the data plays an important role while analyzing it. Storing dates in a specific or understandable format is very important. In this article, we are going to learn how we can specify a Date format on SQL Server. Let's create our de
4 min read
Functions in MySQL | Numeric, String and Date Time Functions in MySQL
In MySQL, functions play a crucial role in performing various operations on data, such as calculations, string manipulations, and date handling. These built-in functions simplify complex queries and data transformations, making it easier to manage and analyze data within a database. In this article, we will look at these different categories of MyS
4 min read
Date manipulating functions in SQL
To manipulate and fetch date values from the table, oracle has inbuilt date functions. Here, we are going to cover Date manipulating functions in SQL. ADD_MONTHS : It will add the total number of months specified in n to the date parameter and Returns date. Syntax : ADD_MONTHS(d, n) Example - Select ADD_MONTHS(SYSDATE, 4) "Add months" from dual; Ou
2 min read
SQL - SELECT DATE
In Microsoft SQL Server, SELECT DATE is used to get the data from the table related to the date, the default format of date is 'YYYY-MM-DD'. Syntax: SELECT * FROM table_name WHERE condition1, condition2,..; Now we will execute queries on SELECT DATE on database student in detail step-by-step: Step 1: Creating a database university: CREATE DATABASE
1 min read
SQL Query to Check Given Format of a Date
Here, we are going to see how to find the SQL Query to check whether the date passed to query is the date of the given format or not. In this article, we will be making use of the Microsoft SQL Server as our database. Here, we will first create a database named “geeks” then we will create a table “department” in that database. After, that we will e
2 min read
SQL Query to Convert Date Range to Per Day Records
In this article, we are going to see how we can convert the date range to per day records, but before that, we need to have a clear idea of where it is going to be helpful for us. Suppose, We have a database where we want to store all the project details records of the students of a particular class. There we can see how long it took a student to c
5 min read
Update Date Field in SQL Server
The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statements as per our requirement. With this article, we will learn how to Update the Date Field in SQL Server. In this article, we will be making use of the Microsoft SQL Server as o
2 min read
SQL Query to Convert Date to Datetime
In this article, we will look at how to convert Date to Datetime. We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type.Convert() function is used to convert a value of any ty
1 min read
SQL Query to Convert Date Field to UTC
In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. In this article, we will discuss how to convert Date Field to UTC in SQL. For this art
2 min read
SQL Query to Delete a Data From a Table Based on Date
Many of the time we have to delete data based on the date. These dates can be some older dates. For this purpose, we can use delete query along with where clause. This approach helps us to delete some old data in our database. In this article, we are going to delete the data of employees based on their birth dates. Step 1: Creating the database Que
2 min read
How to Pull Data From Two Tables SQL Based on Date?
In this article, we will see, How to write SQL queries to pull data from two tables based on date. We can perform the above function by performing the UNION operation on the tables. UNION operation appends the results of two queries and returns the result as a single set. Ways to use the UNION keyword: UNION ALL: Includes duplicates.UNION: Excludes
2 min read
Comparing Timestamp Dates With Date-Only Parameter in SQL
In this article, we are going to learn a SQL query to compare timestamp dates with date-only parameters. To do this, we will first create a table in a database and add timestamp values to it. After that, we use the SELECT command in SQL with a condition(using WHERE command) to get some values from the table. For comparison, will convert DateTime va
2 min read
SQL Query to Compare Results With Today's Date
To compare query results with today's date, use the CASE() function with the SQL GETDATE() function. SQL GETDATE() function is used to return the present date and time of the database system and the CASE() function can used to compare two dates in SQL. Query to check the current date in SQL: SELECT GETDATE();Output: SQL Compare Results With Today's
1 min read
SQL Query to Find the Year from Date
To find the year from the date, use the YEAR() function in SQL. SQL YEAR() function returns the year component of a date as a four-digit number in the range 1900-9999. SyntaxFind Year from Data in SQL Syntax: SELECT YEAR(<date_string>);ORSELECT YEAR(<column_table>) FROM <table_name>;How to Get the Year From Date in SQLHere, we wil
2 min read
SQL | Date Functions (Set-1)
SQL Date Functions are essential for managing and manipulating date and time values in SQL databases. They provide tools to perform operations such as calculating date differences, retrieving current dates and times and formatting dates. In this article, We will learn about the various SQL Date Functions with the help of examples and so on. SQL Dat
5 min read
SQL | Date Functions (Set-2)
SQL Date Functions are powerful tools that allow users to manipulate, extract, and format date and time values within SQL databases. In this article, We will learn about What are SQL Date Functions along with various examples and so on. What are SQL Date Functions?SQL Date Functions are built-in functions that help manipulate, extract, and format d
4 min read
How to Find Day Name From Date in SQL Server?
Finding the day name from a specific date is a common task in SQL Server, useful for generating reports, analyzing trends, or scheduling. SQL Server provides two primary methods such as the DATENAME() function and the FORMAT() function. In this article, We will learn about How to Find Day Name From the Date in an SQL Server in detail and so on. How
4 min read
Article Tags :