search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

MySQL | GET_FORMAT method

schedule Aug 12, 2023
Last updated
local_offer
MySQL
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

MySQL's GET_FORMAT(~) method will return a format string based on the default formatting style for the input region.

Parameters

1. DATE | TIME | DATETIME

The data type to retrieve the format string for.

2. 'EUR' | 'USA' | 'JIS' | 'ISO' | 'INTERNAL'

The region whose formatting style we should return. Note ISO here refers to ISO 9075.

Return value

The below table shows the possible method calls, together with their corresponding return format string:

Method call

Return value

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'

Examples

Basic usage

To return format string for ISO 9075 date:

SELECT GET_FORMAT(DATE,'ISO');
+------------------------+
| GET_FORMAT(DATE,'ISO') |
+------------------------+
| %Y-%m-%d |
+------------------------+

Usage with DATE_FORMAT(~)

To use the return value of GET_FORMAT(~) as an input for DATE_FORMAT(~)

SELECT DATE_FORMAT('2020-04-26 21:53:10', GET_FORMAT(DATE, 'USA'));
+-------------------------------------------------------------+
| DATE_FORMAT('2020-04-26 21:53:10', GET_FORMAT(DATE, 'USA')) |
+-------------------------------------------------------------+
| 04.26.2020 |
+-------------------------------------------------------------+

Note that the return value of GET_FORMAT(DATE,'USA') is '%m.%d.%Y', which specifies the format we should use to format '2020-04-26 21:53:00'.

List of formatting types

Below is a table with a list of valid formatting types:

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; WEEK() mode 0

%u

Week (00..53), where Monday is the first day of the week; WEEK() mode 1

Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X

%v

Week (01..53), where Monday is the first day of the week; WEEK() mode 3; 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

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!