3

I want make a select that adds decimal point into integers, but when I do it, it shows me bunch of # instead of those numbers.

SELECT to_char(1234, '99.99');

What I expected was table with a value of 12.34 but I got ##.## in my select. However, if I did

SELECT to_char(1234, '99,99');

it showed be 12,34 as expected. Problem is, that I want to have a decimal point and not a comma. I am using PostgreSQL 13.2

1
  • That makes no sense. 1234 has no digits after the decimal point. It would work for 12.34. Commented May 20, 2021 at 14:35

2 Answers 2

1

It seems you want to take the last 2 digits and pretend they were decimals. You can't use the predefined . or D formats because they apply to true decimals.

Instead, you can print the dot character (like any other string), between double quotes, before the last 2 digits:

 SELECT to_char(1234, '999"."99');
 to_char
---------
   12.34

PS: on a side note, you are getting the masked output in your 1st query because there isn't enough digit positions on the format:

 SELECT to_char(1234, '9999.99');
 to_char
----------
  1234.00
Sign up to request clarification or add additional context in comments.

Comments

1

Welcome to SO. You were very close:) Which locale are you using? Check this example for en_US.UTF-8:

SELECT to_char(1234, '999G99');
 to_char 
---------
   12.34
(1 row)
  • G: Group separator that uses locale
  • D: Decimal point that uses locale

Check this tutorial

2 Comments

I tried your select, and it says "12NBSP34" or just "12 34" with a space in between. If you mean numeric locale, then I use Slovak_Slovakia.1250
I see, this one I couldn't predict :D But as I can see, the post from JGH already answers your question. Cheers :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.