How to Strip Time Component From Datetime in MySQL?
Last Updated :
19 Mar, 2024
MySQL is a very popular open-source and free database server that is ideal for small and large applications and is a relational database management system where SQL (Structured Query Language) queries or statements are used to manage and manipulate the data.
MySQL provides many built-in functions like string, date, and numeric functions. Date functions allow you to operate on the date strings like getting the current date, calculating the difference between two dates, extracting the date, year, month, hour, or time component from the date, etc. These built-in functions help you to perform tasks and operations on 'Date strings' easily.
MySQL Strips Time Component from Datetime
Now, extracting only date or time from date can be performed easily by using some built-in functions in MySQL. To extract only the Date component from datetime we can use the following functions,
- Using DATE() function
- Using CAST() function
- Using DATE_FORMAT() function
In this article, we explore three essential methods—DATE(), CAST(), and DATE_FORMAT()—for extracting date components from datetime strings in MySQL effortlessly.
1. Using DATE() function
The DATE() function takes a datetime expression as input and returns only the date component.
Syntax:
SELECT DATE (expression);
- The DATE() function takes the date or datetime expression as a parameter or argument and returns the date component from the expression. It only takes one argument or parameter and returns null if the expression is invalid or empty.
Example:
Let us take the datetime as '2024-03-15 01:05:46'.
SELECT DATE ('2024-03-15 01:05:46') as date_component;
Output:
Date function Explanation: From the above output, we can observe that only the date component or part from the datetime string is extracted and returned. Similarly, if we provide only the date as a parameter or argument to the date function, the date component is returned i.e., the argument itself is returned as it is the date.
2. Using CAST() function
The CAST() function converts a datetime expression to the DATE datatype, effectively stripping the time component.
Syntax:
SELECT CAST(expression AS DATATYPE);
- The CAST() function converts the expression to the specified datatype. For extracting the date component from datetime we can cast the datetime expression to the date datatype.
Example:
Let us consider the datetime '2020-12-31 23:59:59', we now use the cast function to extract the date component,
SELECT CAST( '2020-12-31 23:59:59' AS DATE) AS date_component;
Output:
CAST() function to extract dateExplanation: The above function also returns the date component from datetime expression where it is converting the datetime expression to date and returning the result and remember to avoid any spaces between the function name and the opening parenthesis.
3. Using DATE_FORMAT() Function
DATE_FORMAT() allows users to specify custom formats for datetime strings, enabling precise extraction of date components.
Syntax:
SELECT DATE_FORMAT(date,format);
- The DATE_FORMAT() function takes two arguments, the date string and the format to which the date needs to be converted and if any of the argument is null, the function returns null.
Example:
Let us consider the same datetime used in above example, '2020-12-31 23:59:59' and we now use the date_format() function to achieve the same result as above.
SELECT DATE_FORMAT('2020-12-31 23:59:59', '%Y-%m-%d') AS date_component;
Output:
DATE_FORMAT() functionExplanation: The above query also returns the same result as in the above example i.e., the date component from datetime the format can be different and is based on what you want to extract or how you want to format the date. Note that different format specifiers are used in the second parameter of the function and the % character is required before format specifier characters.
Conclusion
By using the built-in DATE functions the date analysis operations or filtering tasks can be easily performed. The time component can be stripped from the datetime expression by using the above functions. The DATE() function is efficient and is solely used for extracting the date component from the expression and the other two functions mentioned above return the date component when certain parameters or arguments are specified and the method you want to choose depends on your preference and performance needs.
Similar Reads
How to Convert Timestamp to Datetime in MySQL?
In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret
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 d
2 min read
How to Extract Time from Datetime in R ?
In this article, we are going to extract time from Datetime in R programming language using lubridate() function and format() function. Note: datetime is in the format of time and date (YYYY/MM/DD HH:MM:SS) i.e. Year:month:day Hours:Minute:Seconds Where, Year:month:day comes under dateHours:Minute:S
3 min read
How to change datetime to string in SQLAlchemy query?
In this article, we are going to change DateTime to string in sqlalchemy query in the python programming language. Database used: Installation Syntax to install sqlalchemy and pymysql: pip install sqlalchmey pymysql Note: pymysql is a dependency of sqlalchemy which we need to install for this post F
2 min read
SQL Query to Convert Datetime to String
In order to convert a DateTime to a string, we can use CONVERT() and CAST() function. These functions are used to converts a value(of any datatype) into a specified datatype. CONVERT() Function Syntax: CONVERT(VARCHAR, datetime [,style])VARCHAR - It represent the string type.datetime - It can be the
3 min read
How to Get Current Date and Time in SQL?
Managing date and time efficiently is crucial for any database system. SQL provides built-in functions to retrieve the current date and time, which are especially useful for applications involving logging, reporting, and auditing. In this article, we will explain the three main SQL functions to fetc
4 min read
Convert string to datetime in Python with timezone
Prerequisite: Datetime module In this article, we will learn how to get datetime object from time string using Python. To convert a time string into datetime object, datetime.strptime() function of datetime module is used. This function returns datetime object. Syntax : datetime.strptime(date_string
3 min read
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read
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
1 min read
How to Extract time from timestamp in R ?
In this article, we are going to see how to extract time from timestamp in the R Programming language. Method 1: Using POSIXct class in R We can store a date variable in the form of a string variable, and then convert it to a general format timestamp. POSIXct method can be used which converts a date
4 min read