Date & Time Functions In mySQL

MySQL CURDATE ()Function

CURDATE() returns the Current date.

Syntax:

CURDATE()

Example:

The following SELECT statement:

SELECT NOW(),CURDATE(), CURTIME()

MySQL CURTIME() Function

CURTIME() returns the Current time.

Syntax: CURTIME()

Example:

The following SELECT statement:

SELECT NOW(),CURDATE(),CURTIME()

MySQL DATE() Function

Definition and Usage:

The DATE() function extracts the date part of a date or date/time expression.

Syntax:

DATE(date) Where date is a valid date expression.

EXAMPLE:

SELECT Product Name, DATE(OrderDate) AS OrderDate FROM Orders

WHERE Orderld=1

MySQL DATEDIFF() Function

Definition and Usage The DATEDIFF() function returns the time between two

 dates.

Syntax DATEDIFF(date1,date2)Where date1 and date2 are valid date or

 date/time expressions.

Example:

The followingSELECT statement: 

SELECT DATEDIFF(2011-09-25,2011-09-24) AS DiffDate

MySQL DATE_ADD() Function

Definition and usage of the DATE_ADD() the function adds a specified time interval to a 

date.

EXAMPLE:

SELECT OrderldDATE_ADD(OrderDate INTERVAL 15 DAY) AS OrderPayDate 

FROM Orders

Syntax:

DATE- ADD(date,INTERVAL expr type)

MySQL DATE FORM Function

Definition and Usage

The DATE_FORMAT() function is used to display date/time data in different 

formats

EXAMPLE:

DATE FORMAT(NOWO, %Dh:i p)

DATE_FORMAT(NOW),Zm-%d-%Y’)

DATE FORMAT(NOW0,7.d %b 72y)

DATE FORMAT(NOwO,Zd .b YT:Zf)

Syntax:

DATE FORMAT(date,format)

MySQL DATE SUB() Function

Definition and Usage The DATE_SUB) function subtracts a specified time interval 

from a date.

Syntax:

DATE_SUB ( date, INTERVAL expr type)

EXAMPLE:

SELECT OrderId,DATE SUB(OrderDate,INTERVAL 3 DAY) AS SubtractDate

FROM Orders

Leave a Reply

Your email address will not be published. Required fields are marked *