Sunteți pe pagina 1din 15

SQL DATE and TIME

FUNCTIONS
PROF. RYAN CELIS
NOW()
Returns the current date and time

Syntax:
SELECT NOW()
CURDATE()
Returns the current date

Syntax:
SELECT CURDATE()
CURTIME()
Returns the current time

Syntax:
SELECT CURTIME()
DATE()
Extracts the date part of a date or date/time
expression

Syntax:
SELECT DATE(FIELD) FROM TABLE
EXTRACT()
Returns a single part of a date/time

Syntax:
SELECT EXTRACT(unit FROM FIELD)
FROM TABLE
Unit can be one of the following

UNIT
MICROSECOND MINUTE_MICROSECOND
SECOND MINUTE_SECOND
MINUTE HOUR_MICROSECOND
HOUR HOUR_SECOND
DAY HOUR_MINUTE
WEEK DAY_MICROSECOND
MONTH DAY_SECOND
QUARTER DAY_MINUTE
YEAR DAY_HOUR
SECOND_MICROSECOND YEAR_MONTH
DATE_ADD()
Adds a specified time interval to a date

Syntax:
SELECT DATE_ADD(FIELD, INTERVAL value unit)
FROM TABLE
Unit can be one of the following

UNIT
MICROSECOND MINUTE_MICROSECOND
SECOND MINUTE_SECOND
MINUTE HOUR_MICROSECOND
HOUR HOUR_SECOND
DAY HOUR_MINUTE
WEEK DAY_MICROSECOND
MONTH DAY_SECOND
QUARTER DAY_MINUTE
YEAR DAY_HOUR
SECOND_MICROSECOND YEAR_MONTH
DATE_SUB()
Subtracts a specified time interval from a date

Syntax:
SELECT DATE_SUB(FIELD, INTERVAL value unit)
FROM TABLE
Unit can be one of the following

UNIT
MICROSECOND MINUTE_MICROSECOND
SECOND MINUTE_SECOND
MINUTE HOUR_MICROSECOND
HOUR HOUR_SECOND
DAY HOUR_MINUTE
WEEK DAY_MICROSECOND
MONTH DAY_SECOND
QUARTER DAY_MINUTE
YEAR DAY_HOUR
SECOND_MICROSECOND YEAR_MONTH
DATEDIFF()
Returns the number of days between two
dates

Syntax:
SELECT DATEDIFF(date1, date2)
DATE_FORMAT()
Displays date/time data in different formats

Syntax:
SELECT DATE_FORMAT(date, format)
Formats
FORMAT

Format Description Format Description Format Description

%a Abbreviated weekday name %k Hour (0-23) %v Week (01-53) where Monday is the first day
of week, used with %x
%b Abbreviated month name %l Hour (1-12) %W Weekday name

%c Month, numeric %M Month name %w Day of the week (0=Sunday, 6=Saturday)

%D Day of month with English suffix %m Month, numeric (00-12) %X Year of the week where Sunday is the first
day of week, four digits, used with %V
%d Day of month, numeric (00-31) %p AM or PM %x Year of the week where Monday is the first
day of week, four digits, used with %v
%e Day of month, numeric (0-31) %r Time, 12-hour (hh:mm:ss AM or PM) %Y Year, four digits

%f Microseconds %S Seconds (00-59) %y Year, two digits

%H Hour (00-23) %s Seconds (00-59)

%h Hour (01-12) %T Time, 24-hour (hh:mm:ss)

%I Hour (01-12) %U Week (00-53) where Sunday is the


first day of week
%i Minutes, numeric (00-59) %u Week (00-53) where Monday is the
first day of week
%j Day of year (001-366) %V Week (01-53) where Sunday is the
first day of week, used with %X
Try this. . .
SELECT DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')

S-ar putea să vă placă și