Jump to: navigation, search

Mysql GET FORMAT

From w3cyberlearnings

Contents

MySQL GET_FORMAT Function

This function is very useful when combine with DATE_FORMAT() and STR_TO_DATE() functions . It returns a format string for date.

Format and Syntax

Function Call	                 Result
-----------------------------------------------
GET_FORMAT(DATE,'USA')	        '%m.%d.%Y'
GET_FORMAT(DATE,'JIS')	        '%Y-%m-%d'
GET_FORMAT(DATE,'ISO')	        '%Y-%m-%d'
GET_FORMAT(DATE,'EUR')	        '%d.%m.%Y'
GET_FORMAT(DATE,'INTERNAL')	'%Y%m%d'
GET_FORMAT(DATETIME,'USA')	'%Y-%m-%d %H.%i.%s'
GET_FORMAT(DATETIME,'JIS')	'%Y-%m-%d %H:%i:%s'
GET_FORMAT(DATETIME,'ISO')	'%Y-%m-%d %H:%i:%s'
GET_FORMAT(DATETIME,'EUR')	'%Y-%m-%d %H.%i.%s'
GET_FORMAT(DATETIME,'INTERNAL')	'%Y%m%d%H%i%s'
GET_FORMAT(TIME,'USA')	        '%h:%i:%s %p'
GET_FORMAT(TIME,'JIS')	        '%H:%i:%s'
GET_FORMAT(TIME,'ISO')	        '%H:%i:%s'
GET_FORMAT(TIME,'EUR')	        '%H.%i.%s'
GET_FORMAT(TIME,'INTERNAL')	'%H%i%s'

Example 1

Get the USA date format.

mysql> SELECT GET_FORMAT(DATE,'USA');
+------------------------+
| GET_FORMAT(DATE,'USA') |
+------------------------+
| %m.%d.%Y               | 
+------------------------+
1 row in set (0.00 sec)

Example 2

Return the ISO date format.

mysql> SELECT DATE_FORMAT('2012-4-14',GET_FORMAT(DATE,'ISO'));
+-------------------------------------------------+
| DATE_FORMAT('2012-4-14',GET_FORMAT(DATE,'ISO')) |
+-------------------------------------------------+
| 2012-04-14                                      | 
+-------------------------------------------------+
1 row in set (0.00 sec)

Example 3

mysql> SELECT STR_TO_DATE('2012-4-14',GET_FORMAT(DATE,'ISO'));
+-------------------------------------------------+
| STR_TO_DATE('2012-4-14',GET_FORMAT(DATE,'ISO')) |
+-------------------------------------------------+
| 2012-04-14                                      | 
+-------------------------------------------------+
1 row in set (0.00 sec)


Related Links



Navigation
Web
SQL
MISC
References