How to use the MySQL GET_FORMAT() function

MySQL GET_FORMAT() is a function that returns a date or time format string. It can be used to format a date or time value according to a specific locale or standard.

Posted on

MySQL GET_FORMAT() is a function that returns a date or time format string. It can be used to format a date or time value according to a specific locale or standard. The function takes two arguments: the type of the format and the name of the format. The type can be one of the following values: DATE, TIME, or DATETIME. The name can be one of the following values: EUR, USA, JIS, ISO, or INTERNAL.

Syntax

The syntax of the function is:

GET_FORMAT(type, name)

The parameters are:

  • type: The type of the format. It can be DATE, TIME, or DATETIME.
  • name: The name of the format. It can be EUR, USA, JIS, ISO, or INTERNAL.

Examples

Some examples of using the function are:

  • To get the date format for the European standard, use:

    SELECT GET_FORMAT(DATE, 'EUR');
    

    The result is:

    '%d.%m.%Y'
    

    that the date is formatted as day.month.year, with leading zeros and four-digit year.

  • To get the time format for the ISO standard, use:

    SELECT GET_FORMAT(TIME, 'ISO');
    

    The result is:

    '%H:%i:%s'
    

    This means that the time is formatted as hour:minute:second, with 24-hour clock and leading zeros.

  • To get the datetime format for the Japanese standard, use:

    SELECT GET_FORMAT(DATETIME, 'JIS');
    

    The result is:

    '%Y-%m-%d %H:%i:%s'
    

    This means that the datetime is formatted as year-month-day hour:minute:second, with four-digit year and leading zeros.

  • To format a date value using the USA format, use:

    SELECT DATE_FORMAT('2023-11-15', GET_FORMAT(DATE, 'USA'));
    

    The result is:

    '11-15-2023'
    

    This means that the date is formatted as month-day-year, with leading zeros and four-digit year.

  • To format a time value using the INTERNAL format, use:

    SELECT TIME_FORMAT('10:20:21', GET_FORMAT(TIME, 'INTERNAL'));
    

    The result is:

    '102021'
    

    This means that the time is formatted as hourminutesecond, with no separators and leading zeros.

Similar Functions

Some similar functions to GET_FORMAT() are:

  • DATE_FORMAT(): This function formats a date or datetime value according to a specified format string. It can use any format specifier supported by MySQL, not just the ones returned by GET_FORMAT().
  • TIME_FORMAT(): This function formats a time value according to a specified format string. It can use any format specifier supported by MySQL, not just the ones returned by GET_FORMAT().
  • STR_TO_DATE(): This function converts a string to a date, time, or datetime value, based on a specified format string. It can use any format specifier supported by MySQL, not just the ones returned by GET_FORMAT().
  • FROM_UNIXTIME(): This function converts a Unix timestamp to a date or datetime value, optionally formatted according to a specified format string. It can use any format specifier supported by MySQL, not just the ones returned by GET_FORMAT().