- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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)
Advertisements