EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL FORMAT
Secondary Sidebar
MySQL Tutorial
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

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.

The syntax for this function will be as follow:

FORMAT (column_name, decimal_points)

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

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
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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