|
|
1. Possible style of input params for MySQL date-time functions.
For example date 18th June 1999 can
be passed in any one of the following ways:
19990618,'19990618',
'1999-06-18','1999.06.18',
'1999 06 18'. The following two selects returns
the same result:
SELECT
   YEAR(19990618) AS the_year,
   MONTH(19990618) AS the_month,
   DAY(19990618) AS the_day;
SELECT
   YEAR('1999.06.18') AS the_year,
   MONTH('1999.06.18') AS the_month,
   DAY('1999.06.18') AS the_day;
|
| the_year | the_month | the_day |
| 1999 | 6 | 18 |
2. Function DATE_FORMAT()
Function DATE_FORMAT() formats the date
value according to the format string.
SELECT
UPPER(CONCAT(firstname,' ', lastname)) AS Name,
DATE_FORMAT(DateOfBirth, '%a %D %b %Y') AS WasBorn_On
FROM employees
WHERE empno BETWEEN 8 AND 10;
|
Gives us following list:
| Name | WasBorn_On |
| DONA FIBER | Tue 18th May 1965 |
| LEONARDO ROTA | Tue 27th Nov 1979 |
| AIVA PESH | Sun 10th Mar 1985 |
Format string of Internal date representation on the server is
'%Y%m%d'. If today is
09/12/2007, internal format will return
'20071209'.
There are hundreds of Format string variations that helps to shape out date
representation complemeting different localites and habbits.
Function GET_FORMAT() is used to return
format string according to the input parameter that describes some date-format
style: (INTERNAL|EUR|ISO|USA).
SELECT DATE_FORMAT(CURRENT_DATE,GET_FORMAT(DATE,'EUR')) AS EURO_Date,
DATE_FORMAT(CURRENT_DATE, '%d.%m.%Y') AS '%d.%m.%Y';
SELECT DATE_FORMAT(CURRENT_DATE,GET_FORMAT(DATE,'ISO')) AS ISO_Date,
DATE_FORMAT(CURRENT_DATE, '%Y-%m-%d') AS '%Y-%m-%d';
|
Gives the following result:
| EURO_Date | %d.%m.%Y |
| 09.12.2007 | 09.12.2007 |
| ISO_Date | %Y-%m-%d |
| 2007-12-09 | 2007-12-09 |
The following specifiers may be used in the format string. The “%” character
is required before format specifier characters.
| Specifier | Description |
| %a | Abbreviated weekday name (Sun..Sat) |
| %b | Abbreviated month name (Jan..Dec) |
| %c | Month, numeric (0..12) |
| %D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) |
| %d | Day of the month, numeric (00..31) |
| %e | Day of the month, numeric (0..31) |
| %f | Microseconds (000000..999999) |
| %H | Hour (00..23) |
| %h | Hour (01..12) |
| %I | Hour (01..12) |
| %i | Minutes, numeric (00..59) |
| %j | Day of year (001..366) |
| %k | Hour (0..23) |
| %l | Hour (1..12) |
| %M | Month name (January..December) |
| %m | Month, numeric (00..12) |
| %p | AM or PM |
| %r | Time, 12-hour (hh:mm:ss followed by AM or PM) |
| %S | Seconds (00..59) |
| %s | Seconds (00..59) |
| %T | Time, 24-hour (hh:mm:ss) |
| %U | Week (00..53), where Sunday is the first day of the week |
| %u | Week (00..53), where Monday is the first day of the week |
| %V | Week (01..53), where Sunday is the first day of the week; used with %X |
| %v | Week (01..53), where Monday is the first day of the week; used with %x |
| %W | Weekday name (Sunday..Saturday) |
| %w | Day of the week (0=Sunday..6=Saturday) |
| %X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
| %x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
| %Y | Year, numeric, four digits |
| %y | Year, numeric (two digits) |
| %% | A literal “%” character |
| %x | x, for any “x” not listed above |
|