What is the usage of the SQL FORMAT function?

The FORMAT function in SQL is used to format the display of numbers, dates, and times. It takes two parameters: the value to be formatted and the format string.

The format string consists of specific placeholders used to specify different parts of numbers, dates, or times to display. Here are some common placeholders and their meanings:

  1. Represent the digits that must be displayed, filling in with zeros if the number of digits is insufficient.
  2. Indicate optional digit positions which will not be displayed if not enough digits are provided.
  3. 表示小数点用于显示。
  4. Indicates the thousands separator.
  5. % is the symbol used to represent a percentage.
  6. $: This symbol represents currency.

Here are some examples:

  1. Format numbers:
  2. Format the number 12345.6789 as 12,346.
  3. Format the number 12345.6789 to display as 12345.68.
  4. 12345.6789 displayed as 1234568.79%
  5. Format the date and time:
  6. Change the current date to the format ‘dd/MM/yyyy’ and display as 01/01/2022.
  7. Convert the current time to the format of 12-hour clock: 12:34:56 AM.

The FORMAT function can be applied to various types of numbers, dates, and times, with the specific usage and available format strings depending on the support of the database system.

bannerAds