

The function takes this format: DATE_FORMAT(date, format) It takes in the date to be formatted, and also a string representing the desired format. This function lets you present your data anyhow you want it.
#Mysql like date syntax how to#
How to Use the DATE_FORMAT Function in SQL You can use the DATEDIFF to get this value easily. `For example, in many blogs, you see a part that says something like 'Posted 7 days ago'. This is very useful when you have to return data with respect to time. You can use this function with the ABS function to get the absolute value and not have issues with the negative sign or value. Rearranging the dates and calling the function again results in a difference in the response: SELECT DATEDIFF(CURRENT_DATE, '') Let's use DATEDIFF to find the number of days between today and. It takes in the two dates to be subtracted. This function returns the number of days between two dates. In a songs table with a released field, to fetch all songs released in the last three weeks except for the ones released this week, use ADDDATE like this: SELECT *īETWEEN ADDDATE(CURRENT_DATE, INTERVAL -3 WEEK)ĪND ADDDATE(CURRENT_DATE, INTERVAL -1 WEEK) How to Use the DATEDIFF Function in SQL WHERE hiredate > ADDDATE(CURRENT_DATE, INTERVAL -1 YEAR) Īnother common case would be when you have to filter by a time range. To see all employees that resumed in the past year (where hiredate > the date of a year ago), use ADDDATE like this: SELECT * Then, we called it again to get the date of 3 weeks before that time.Ī common use case of ADDDATE in real life applications is to get data values to be used in a WHERE clause as a range.įor example, if you had an employees table with a hiredate field that stores their resumption date. Here, we called the ADDDATE function twice. To get the date of the day 7 months and 3 weeks ago, use the ADDDATE like this: SELECT ADDDATE(ĪDDDATE(CURRENT_DATE, INTERVAL -7 MONTH), Using ADDDATE, you can find the date of 45 days from today like this: SELECT ADDDATE(CURRENT_DATE, INTERVAL 45 DAY) This functions works exactly the same as the DATE_ADD and you can use them interchangeably. Check the MySQL docs for more possible values. The unit can also be a smaller value like second or even microsecond. Finally, you have the unit, which can be day, week, month, quarter or even year. And the INTERVAL is a constant keyword that has to come before the expr that is used to set the value of the increment in numbers. Here, the date/expr refers to the base date value to be added to or subtracted from. The function takes this format: ADDDATE(date/expr, INTERVAL expr unit) This interval can be positive or negative. It takes an interval that can be in days, or months, or even years. This functions performs additions, or subtractions, on date values. This function has synonymous functions that work just the way it does: CUR_DATE and CURRENT_DATE() will return the exact same result as CURRENT_DATE. It is one of the simplest MySQL functions to use.

This function returns today's date in the format ' YYYY-MM-DD'. How to Use the CURRENT_DATE Function in SQL So your results on running the queries here might be slightly different based on when you read it.

Some perform simple tasks like adding days to dates, finding how many days are between two dates, or even more complicated tasks like how to tell how far into a year a date is by number of days.īefore proceeding, keep in mind that this article was written on. These functions help perform various tasks. In this article, you'll learn how MySQL functions help make date management very easy. These tables can contain a wide range of data types. SQL databases contain tables, which contain rows of data. SQL is a programming language we use to interact with relational databases.
