Asked 3 years ago
22 Sep 2021
Views 719
Mafalda

Mafalda posted

MySQL CURDATE() Function

ajamil

ajamil
answered Sep 22 '21 00:00

MySQL CURDATE() Function gives the current date of the database server.


Example

select CURDATE()


it will give you 2021-09-22 which is today's date when I am answering.
in short MySQL CURDATE() Function give you current date of the database server not client server

Retrun Value :
CURDATE() return date in format YYYY-MM-DD (string return) or YYYYMMDD (numeric return)


so if you invoke CURDATE() then it will return string date format : YYYY-MM-DD .
if you invoke CURDATE() with any arithmetic operation then it will return numeric date format : YYYYMMDD .


CURDATE() with arithmetic operation example :

select CURDATE()+1


CURDATE()+1 means one date after today 's date.
it also CURDATE()+1 return in numeric format YYYYMMDD .

Practical use of the CURDATE() :
suppose you have an "orders" table which has all orders. this table has the order_date field which contains the date of the order, and if you want to list the order placed today.

so query to find all orders placed today will be:


select * from orders where order_date=CURDATE();


in the above query, It will list all orders which have order_date is CURDATE().

so query to find the all orders placed Yesterday will be:


select * from orders where order_date=CURDATE()-1;



CURDATE()-1 return yesterday date in YYYYMMDD format .above query only work when order_date had field type 'DATE'


please note that it will return the database server's date, not the date of the server or client side's date.
Post Answer