MySQL Date Subtract
MySQL has a variety of operations that are beneficial to using and maintaining a relational database. It has various built-in functions that are useful for using the tool.
These functions ease human efforts by taking parameters and providing the results for the dedicated functionality.
These functions include a variety of operations on strings, numbers, dates, and advance functions in MySQL. Examples of such functions include concat
, reverse
, position
, min
, max
, and truncate
.
Subtract Date in MySQL
MySQL provides functions to change and manage dates and times as per needs in the tool. The following are some date time utility functions.
- The
Date
function extracts the data in theDDMMYYYY
format from theDateTime
string. - The
DayOfMonth
function returns the daypart from the complete date after making the internal extraction process. - The
Date_Add
function returns the added date and time interval from a given date. - The
Date_Sub
function returns the subtracted date and time interval from a provided date.
Query to execute the MySQL statement:
select * from student where DOB between DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW();
-
The above query selects all dates of birth between the current date and the last 30 days. The
select
get preceded with the*
asterisk symbol, which means all the records from the filtered list. -
The
from
keyword specifies the user to provide the table name from which we apply the condition or filter. -
The use of the
where
keyword acts as a conditional operator. Here all the conditions are specified to show the particular results. -
The result gets captured from the
Student
table after filtering out the results frombetween
queries. -
The
between
operator returns the selected values from the given range, and the values can be the part of integers, strings, or dates. The above query returns the inclusive date range, which means the first and last values are part of the date range. -
The
now
function returns the date-time inYYYY-MM-DD HH-MM-SS
format. -
The
Date_Sub
function takes two arguments, where the first value denotes the value to be changed. The second argument is the value in date or time that should get subtracted.The values can be minute, second, hours, days, and many more.
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
LinkedIn