EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL FORMAT

MySQL FORMAT

MySQL FORMAT

Introduction to MySQL FORMAT

The following article provides an outline for MySQL FORMAT. FORMAT function is used to format a numerical value in the table to any desired decimal points. Suppose the table columns have varying decimal points as to one rounded off to 2 places, another column rounded off to 4 places. And in the resulting output, we have to get a product of these two columns. In such cases, the output can have varying decimal places. Each row possessing different decimal points from each other can cause much ambiguity. So, in the result SELECT query, we can directly use the FORMAT function and specify to how many decimal points the output should be rounded off to. This is valid in scenarios where the output variable is not a pre-defined one.

Syntax of MySQL FORMAT

FORMAT function is used to format any numerical value in our output.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The syntax for this function will be as follow:

FORMAT (column_name, decimal_points)

Here, the column_name is the field that is to be rounded off and the decimal_points is to how many places the value be rounded off to.

On a detailed note, the syntax can include one more optional field.

FORMAT (column_name, decimal_points, locale)

Here, the field locale refers to the separators and groupings. This is an optional field and is not defined explicitly, then, en_US locale is used by default. More than 100 locales are defined in MySQL.

How FORMAT Function works in MySQL?

Given below shows how the FORMAT function works in MySQL:

Example #1

Let’s format a value with more decimal places to just two.

Code:

SELECT FORMAT(1500.26543,2) value;

The query says to round off the value 1500.26543 to 2 places instead of 5 decimal places. Hope you are familiar with the principles of rounding off as to consider values above 5 and below 5. So here, the expected output is 1500.27 (because the third digit after the decimal point is 5).

Output:

MySQL FORMAT 1

Example #2

Let’s see another example as below.

Code:

SELECT FORMAT(1500.26111,2) value;

So here, the third digit after the decimal point is less than 5, and thus the expected output is 1500.26.

Output:

MySQL FORMAT 10

Example #3

The FORMAT function also adds extra zeros to extend the decimal places.

Code:

SELECT FORMAT(1500.2,2) value;

The value is 1500.2 and has only one decimal place. But the result is to have 2 decimal places. So, an extra zero will be appended by this query.

Output:

adds extra zeros to extend the decimal places

Example #4

Let’s now consider a table inventory with data as No. of items in stock and rate per unit for each item. The result output in this scenario is the total stock value which will be a product of No. of units and rate per unit for each item in stock.

Let’s consider the table as below:

Code:

select * from inventory;

Output:

MySQL FORMAT 4JPG

We will first see the result without using the FORMAT function.

Code:

SELECT itemName, instockValue * PricePerItem ProductCost FROM inventory;

The result output is to display itemName and ProductCost columns where the ProductCost column is a product of inStockValue and PricePerItem.

Output:

MySQL FORMAT 5

So, the output will look like this. It certainly looks clumsy and very inappropriate.

We can use the FORMAT function and retrieve a better-looking output now.

Code:

SELECT itemName, FORMAT (instockValue * PricePerItem, 2) ProductCost
FROM inventory;

We have specified the decimal places to be rounded off to 2 in this case.

Output:

MySQL FORMAT 6

If we compare the outputs of this query and the previous query, we can understand that the latest output is of more clarity.

We can round off the decimal places to zero also.

Code:

SELECT itemName, FORMAT (instockValue * PricePerItem, 0) ProductCost
FROM inventory;

Output:

round off the decimal places to zero

Here, the output is rounded off to zero decimal places. We can specify the decimal places to be 0, or if we leave it blank, the default value considered is 0.

Example #5

We spoke about using locale in the FORMAT query. By default, the locale is en_US. Let’s try any other locale and identify the difference.

We will see two examples for locales.

a. This query is to update the output as Spanish in Spain.

Code:

SELECT itemName, FORMAT (instockValue * PricePerItem, 2, 'es_ES') ProductCost
FROM inventory;

Output:

Spanish in Spain

b. The decimal point is replaced by comma ‘,’ instead of dot ‘.’ in this locale. And there is no grouping thousands in this format.

Let’s try another locale

Code:

SELECT itemName, FORMAT (instockValue * PricePerItem, 2, 'de_DE') ProductCost
FROM inventory;

Output:

MySQL FORMAT 9

In this locale, the comma (,) is used as decimal point and dot (.) to group thousands. This is reverse of default locale en_EN.

Conclusion

In this article, we saw the syntax and examples of FORMAT function. FORMAT function is used to display an output in the desired format with proper decimal places defined. This is used in the SELECT query and the syntax includes the numeric to be formatted, the number of decimal places, and optionally the locale can also be specified. An important point to be noted is that the data type of the values is to be decimal. Because in INT data type, the decimal places already do not hold valid. The default locale value is en_EN.

Recommended Articles

This is a guide to MySQL FORMAT. Here we discuss the introduction along with how FORMAT function works in MySQL? respectively. You may also have a look at the following articles to learn more –

  1. MySQL Index
  2. MySQL DECODE()
  3. MySQL Date Functions
  4. MySQL Boolean
C++ PROGRAMMING Certification Course
38+ Hours of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Certification Course
149+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL - Everything in SQL
253+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Certification Course
74+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MYSQL Certification Course
 115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Hurry! - Any Learning Path @ $19 | OFFER ENDING IN ENROLL NOW