MySQL Date Subtract

Rashmi Patidar Feb 15, 2024
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.

  1. The Date function extracts the data in the DDMMYYYY format from the DateTime string.
  2. The DayOfMonth function returns the daypart from the complete date after making the internal extraction process.
  3. The Date_Add function returns the added date and time interval from a given date.
  4. 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();
  1. 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.

  2. The from keyword specifies the user to provide the table name from which we apply the condition or filter.

  3. The use of the where keyword acts as a conditional operator. Here all the conditions are specified to show the particular results.

  4. The result gets captured from the Student table after filtering out the results from between queries.

  5. 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.

  6. The now function returns the date-time in YYYY-MM-DD HH-MM-SS format.

  7. 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.

Image to create and manipulate on date functions

Rashmi Patidar avatar Rashmi Patidar avatar

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

Related Article - MySQL Date