0

I have a table called products. It stores the name of products and their price. The price is stored as cents in the format of an integer.

When I select my data:

  SELECT name, price 
  FROM products

I get data like this:

  Name    Price
  Car     3032
  Banana  178

But I would like the data to be formatted like this:

  Name    Price
  Car     30,32
  Banan   1,78

Is this possible? I only now functions like sum,count.. etc

6
  • 2
    This isn't formatting, you are changing the value. If you want to store a decimal in the database store a decimal not an integer. You should use the proper data type for the job. Why are you storing a decimal as an integer anyway? Are you worried about loss of precision in floats, do you have legacy data? Jan 9, 2015 at 17:12
  • not sure I agree with you @PanagiotisKanavos there are many reasons to store currency as integers.
    – Hogan
    Jan 9, 2015 at 17:14
  • I suppose it depends on if you ever expect your bananas to cost huge, inordinate amounts of money...or i suppose very small amounts of money (though you'd still run into issues with small amount of money too, if you're only storing to the cent value...). Jan 9, 2015 at 17:15
  • 1
    @Hogan Haven't encountered any valid reasons in the last 15 years of working on data warehouses etc. I have encountered attempts to solve unrelated errors (like the lack of a proper decimal type, coder confusion about the differences between numeric and float types) that somehow became legacy. A numeric(9,2) unambiguously defines the precision of a number while an integer value depends on interpretation by the application Jan 9, 2015 at 17:15
  • @PanagiotisKanavos - ok, how about floating point rounding when using floats to represent currency. Have you ever seen that introduce issues. If so then that is a case that would have been better served with an integer. I will agree that numeric(9,2) is the best choice if you have that feature available.
    – Hogan
    Jan 9, 2015 at 17:18

2 Answers 2

7

How about

SELECT name, price/100 as price FROM products
1
  • Division, an underused feature of SQL 8)
    – Hogan
    Jan 9, 2015 at 17:13
1

The big problem I see here is that you want to use a comma. I don't know of a wrapper class that does this. However, it is possible to solve your problem like this. Normally I would say that you should convert to a double and divide, but that isn't really needed here. What you need is some math to get the value of every digit.

(price - price%1000)/1000

The modulus will remove the excess not divisible by 1000. After division you will have 3 for the Car example you gave. Some implementations won't need the modulus part of the equation and will round it properly. I don't like to trust these things. Things get a little trickier after the first digit.

You need to declare 4 variables to hold each place as you work through the number. If you want prices higher than 99.99 you need more places. lets say you have int A, B, C, and D. For simplicity's sake I will also use a variable temp.

A = (price - price%1000)/1000
temp = price - (A*1000)
B = (temp - temp%100)/100
temp = temp - (B*100)
C = (temp - temp%10)/10
temp = temp - (C*10)
D = temp

Using this procedure you can get the number in each place of the integer into A, B, C, and D. The temp variable has the part removed after you calculate that place. This allows the same math function to work for the next place. Really you can do this without temp but it might needlessly confuse a user of your code (or yourself lol). Now that you have the places you can put them piecemeal into whatever output you want.

std::cout<<"$"<<A<<B<<","<<C<<D;

That is just an example for C++. Ideally you would also check if A, B, ect. are 0 and account for that.

1
  • this is overkill... just use culture specific formatting in the client software
    – Hogan
    Jan 12, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.