MYSQL - Thousands separator

社会主义新天地 提交于 2019-12-12 09:34:01

问题


I would like to know if there is a way to get a thousand separator in a SQL Query ?

As i'm a bit lazy, I want to build a request which can allow me to copy/paste the result to my boss without adding this separator :D

The request looks like this :

SELECT  COALESCE(Customer, 'TOTAL') AS "Customer", CONCAT( COUNT( SegmentId ) , ' bookings' ) AS "Nb bookings", CONCAT( REPLACE( SUM( Price ) , '.', ',' ) , ' €' ) AS "Total (€)", CONCAT( ROUND( (
SUM( Price ) / (
SELECT SUM( Price )
FROM my_db
WHERE CreationDate = CURRENT_DATE( )
AND SegmentStatus = "OK" ) *100 ) , 2 ) , ' %'
) AS "PDM"
FROM my_db
WHERE CreationDate = CURRENT_DATE( )
AND SegmentStatus = "OK"
GROUP BY Customer
WITH ROLLUP

Currently, the result is (table with ';' separator, sorry I didn't manage to make a table with this editor :( ) :

Customer;Nb bookings;Total (€);PDM

cust_1;20 bookings;20000 €;10,01 %

cust_2;254 bookings;17852,12 €;8,12 %

What I want is a result like that :

Customer;Nb bookings;Total (€);PDM

cust_1;20 bookings;20 000 €;10,01 %

cust_2;254 bookings;17 852,12 €;8,12 %

Is there a way to do so ?

Thank,

B


回答1:


I don't know how to do it with a space, but the standard (with a point or a comma [germany i.e.]) seperation can be achieved with FORMAT().

Have a look at the Format Function

mysql> SELECT FORMAT(12332.123456, 4);
    -> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
    -> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
    -> '12,332'
mysql> SELECT FORMAT(12332.2,2,'de_DE');
    -> '12.332,20'
mysql> SELECT FORMAT(12332.2,2,'pt_BR');
    -> '12332,20'


来源:https://stackoverflow.com/questions/6611362/mysql-thousands-separator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!