
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to format number with “.” as thousand separators, and “,” as decimal separator?
You can use format() function for the separators. It will work in MySQL version greater than or equal to 5.5. We are using the version 8.0.12
mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec)
The syntax is as follows
SELECT FORMAT(yourColumnName, valueAfterDecimalPoint, 'de_DE') AS anyAliasNamefrom yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table formatNumberDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Number DECIMAL(19,1) -> ); Query OK, 0 rows affected (1.13 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into formatNumberDemo(Number) values(10000000000000.2); Query OK, 1 row affected (0.15 sec) mysql> insert into formatNumberDemo(Number) values(1000.4); Query OK, 1 row affected (0.21 sec) mysql> insert into formatNumberDemo(Number) values(1000000.6); Query OK, 1 row affected (0.11 sec) mysql> insert into formatNumberDemo(Number) values(100000000.7); Query OK, 1 row affected (0.17 sec) mysql> insert into formatNumberDemo(Number) values(100000000000000000.8); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from formatNumberDemo;
The following is the output
+----+----------------------+ | Id | Number | +----+----------------------+ | 1 | 10000000000000.2 | | 2 | 1000.4 | | 3 | 1000000.6 | | 4 | 100000000.7 | | 5 | 100000000000000000.8 | +----+----------------------+ 5 rows in set (0.00 sec)
Here is the query to format number with '.' as thousand separator, and ',' as decimal separator
mysql> SELECT FORMAT(Number, 3, 'de_DE') AS Numberformat from formatNumberDemo;
The following is the output
+-----------------------------+ | Numberformat | +-----------------------------+ | 10.000.000.000.000,200 | | 1.000,400 | | 1.000.000,600 | | 100.000.000,700 | | 100.000.000.000.000.000,800 | +-----------------------------+ 5 rows in set (0.00 sec)
- Related Questions & Answers
- Explain import “as” and Export “as” constructs in JavaScript.
- HTML5 <input type=“file” accept=“image/*” capture=“camera”> display as image rather than “choose file” button
- Render ASP.NET TextBox as HTML5 Input type “Number”
- How to replace “and” in a string with “&” in R?
- Can we use “rank” as column name with MySQL8?
- Header files “stdio.h” and “stdlib.h” in C
- Python Object Comparison “is” vs “==”
- C# Exponential (“E”) Format Specifier
- C# Numeric (“N”) Format Specifier
- How to work with “!=” or “not equals” in MySQL WHERE?
- How to create a dialog with “yes” and “no” options in JavaScript?
- MongoDB Query for boolean field as “not true”
- Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
- How to convert C# DateTime to “YYYYMMDDHHMMSS” format?
- Using combination of “AND” and “OR” in SELECT in ABAP
Advertisements