Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL format number output , thousands separator

I have a mySQL query which is outputting decimal fields with a comma.

SELECT Metals.Metal, FORMAT(Fixes.GBPam, 3) AS AM, FORMAT(Fixes.GBPpm, 3) AS PM,     
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id

Fields GBPam and GBPpm are both of type decimal(10,5)

Now I want columns AM and PM to be formatted to 3 decimal places in my sql query - Correct

I want values in the thousands to be formatted as xxxx.xxx and not x,xxx.xxx - Incorrect

Example output from mysql query:

Metal       AM          PM          Date
Gold        1,081.334   NULL    11-09-12
Silver      21.009      NULL    10-09-12
Platinum    995.650     NULL    11-09-12
Palladium   416.700     NULL    11-09-12

Can you see that output for Gold AM is 1,081.334? How can I get it to output 1081.334?

This is a pain in the ass for me because I have to then muck about in PHP to remove the comma. I would prefer to just get mysql to format it correctly.

like image 291
Gravy Avatar asked Sep 05 '25 03:09

Gravy


2 Answers

Just use ROUND, this is a numeric function. FORMAT is a string function

ROUND(Fixes.GBPam, 3)
like image 87
Imre L Avatar answered Sep 07 '25 21:09

Imre L


you can use replace command for this purpose.

 REPLACE(Fixes.GBPam,',','')

EDIT: With respect to your question you could do something like this:

SELECT Metals.Metal, ROUND(REPLACE(Fixes.GBPam,',',''),3) AS AM,
  ROUND(REPLACE(Fixes.GBPpm,',',''),3) AS PM,     
  DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
  FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id 
like image 34
heretolearn Avatar answered Sep 07 '25 19:09

heretolearn