MySql EXTRACT()/NOW() – CONVERT() – DATEADD() – FORMAT() – Functions

MySOL EXTRACT() Function

Definition and Usage The EXTRACT ()function is used to return a single part of a 

date/time, such as year, month, day, hour, minute, etc.

Syntax:

EXTRACT(unit FROM date)

EXAMPLE:

The following SELECT statement: SELECT EXTRACT (YEAR FROM OrderDate) 

AS OrderYear, EXTRACT(MONTH FROM OrderDate) AS 

OrderMonth,EXTRACT(DAY FROM OrderDate) AS OrderDay,FROM Orders 

WHERE OrderId=1

MySQL NOW() Function

Definition and Usage:

NOW() returns the current date and time.

Syntax:

NOW()

Example:

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

SQL Server CONVERT() Function

Definition and Usage

The CONVERT() function is a general function for Converting data into a new 

datatype and it can be used to display date/time data in different formats.

Syntax:

CONVERT(data type(length),data to_be_Converted,style).

EXAMPLE:

CONVERT(VARCHAR(19),GETDATE())

CONVERT(VARCHAR(10),GETDATE(),110)

CONVERT(VARCHAR(11),GETDATE(),106)

CONVERT(VARCHAR(24),GETDATE(),113)

SQL SERVER DATEADD()FUNCTION

DEFINITION AND USAGE:

The DATEADD() function adds or subtracts a specified time interval from a date

SYNTAX:

DATEADD (datepart,number,date)

EXAMPLE:

SELECT Order ld,DATEADD(day,20,0rderDate) AS OrderPayDate FROM Orders

RESULT:

Orderld.            OrderPayDate

    1                2011-12-2711:41:36.752

MYSQL DATE- FORMAT() FUNCTION

DEFINITION AND USAGE The DATE_ FORMAT() function is used to display 

date/time data in different formats.

SYNTAX

DATE _FORMAT(date,format)

RESULT

Dec 21 2011 09:31 AM

12-21-2011

21 Dec 11

21 Dec 2011 09:3t:48:567

MYSQL NOW() FUNCTION

DEFINITION AND USAGE

NOW() returns the current date and time.

SYNTAX

NOW()

EXAMPLE

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

RESULT:

        NOW().                              CURDATE().              CURTIME()

  2011-12-07 11:41:36.               2011-12-07.                 11:41:36

SQL SERVER CONVERT() FUNCTION

DEFINITION AND USAGE:

The CONVERT() function is a general function for converting data into a new 

datatype and it can be Used to display date/time data in different formats.

SYNTAX:

CONVERT(data _type( length),data to _be Converted, style)

EXAMPLE:

CONVERT(VARCHAR(19) ,GETDATE())

CONVERT(VARCHAR(10),GETDATE(),110)

CONVERT(VARCHAR(11),GETDATE(),106)

CONVERT(VARCHAR(24),GETDATE(),113)

RESULT:

Dec 21 2011 09:31 AM

12-21-2011

21 Dee 11

21 Dec 2011 09:31:48:567

Leave a Reply

Your email address will not be published.