Looking for examples of CURDATE?
If you have doubts about how to use the CURDATE function in MySQL or you want an example of copy and paste this post is for you.
Examples, more examples and a bit, just a bit, of theory to fully learn this SQL function.
Content
- How do you use CURDATE SQL?
- MySQL version requirements
- CURDATE SQL Examples
How do you use CURDATE SQL?
How is it possible that you already know, CURDATE is a SQL function and specifically, available in MySQL, which returns the current dateyes, the date and only the date (without the time) of the moment it is executed.
Also, the date will be returned in the standard SQL format ‘YYYY-MM-DD’ or YYYYMMDD. The first result if it is used in a string/text type context and the second in the case of a numeric context.
In this way you should know that there are 2 synonymous functions and that they perform the same task, these are:
- SAVE()
- CURRENT_DATE()
- CURRENT_DATE: in this case without parentheses, works too.
That being said, promise is debt, so I’ll go with the first example. You could run a SQL query like the following:
SELECT CURDATE(), CURDATE() + 0;
And receive a response result such as the following:
SAVE() | SAVE() + 0 |
---|---|
2023-02-21 | 20230221 |
MySQL version requirements
To use this function you will need a MySQL version of 4.0 or older.
You shouldn’t have any problems, we are currently on version 8+.
CURDATE SQL Examples
Here are 4 examples:
- SELECTSQL super simple that shows the date from 30 days ago (so you can see how it is combined with another function).
- SELECTSQL to obtain the orders from a felling “ps_orders” whose date is greater than today, that is, they are from today.
- SQL UPDATE which updates the date of update date_upd with CURRENT_DATE (same as CURDATE()) and the country to ‘ES’. This, of all those edited between the current date and 30 days ago.
- INSERT SQL of an order with the name and email data with the current date.
#para extraer 30 días a la fecha actual. SELECT DATE_SUB(CURDATE(), INTERVAL 30 DAY); #obtener los pedidos de hoy SELECT customer_name, customer_email FROM ps_orders WHERE date_add > CURDATE(); #mover al país ES los pedidos con fecha de los últimos 3 días. UPDATE ps_orders SET date_upd = CURRENT_DATE(), country = 'ES' WHERE date_add BETWEEN NOW() AND DATE_SUB(CURDATE(), INTERVAL 30 DAY); #insertar pedido con fecha de hoy INSERT INTO ps_orders (customer_name, customer_email, date_add) VALUES ('Javier Gómez', '[email protected]', CURDATE() );