Format query to show floating decimal in Oracle SQL - sql

I am using Oracle SQL Developer and I have a query in which one of the columns has a number value that I want to divide by 1,000,000 then display to show commas and up to 10 decimal places if the calculate value has any decimal places. I am currently using this query:
to_char(value/1000000, 'FM999,999,990.9999999999') as Millions_Value
The above format gives me commas and retains the number of decimal places I want, but it unfortunately adds a decimal point at the end of whole numbers as well.
Example:
value/1000000 = 33993
formatted value = 33,993.
what I want = 33,993
For a decimal value, I get what I want and need to keep the format
Example:
value/1000000 = 0.158739
formatted value = 0.158739
value/1000000 = 10.82
formatted value = 10.82
And ideas on how to get rid of the decimal for integers?

The easiest way is to use RTRIM, e.g.
RTRIM(to_char(value/1000000, 'FM999,999,990.9999999999'), '.') as Millions_Value
will remove any trailing periods.
EDIT
If you want to get fancy and internationalize it you need to query NLS_SESSION_PARAMETERS to get the decimal separator, use the G (group) and D (decimal) specifiers in the format string, and then trim off any trailing decimal separator characters. Or in other words:
WITH cteData AS (SELECT 123456789.123456789 AS N FROM DUAL UNION ALL
SELECT 123 FROM DUAL),
cteDecimalSeparator AS (SELECT SUBSTR(VALUE, 1, 1) AS DECIMAL_SEPARATOR
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_NUMERIC_CHARACTERS')
SELECT TO_CHAR(N, 'FM999G999G990D9999999999') AS OLD_FORMAT,
RTRIM(TO_CHAR(N, 'FM999G999G990D9999999999'), s.DECIMAL_SEPARATOR) AS NEW_FORMAT
FROM cteData d
CROSS JOIN cteDecimalSeparator s
which returns
OLD_FORMAT NEW_FORMAT
123,456,789.123456789 123,456,789.123456789
123. 123
Remember: there's no kill like overkill :-)

Personally, I think it looks good with one trailing 0, so converting your format string to have a 0 after the decimal. But to answer you question, I think you just need to trim it after you convert the number like this:
SELECT TRIM(TRAILING '.' FROM TO_CHAR('345676', 'FM999G999G990D9999999999'))
FROM DUAL;

Related

How can I format the decimal value without decimal?

eg 18.45 should be 00000000001845000
datatype suppose number(x,5) so last five digits are for precision
Another option is to use the V format model element; from the documentation:
Element
Example
Description
V
999V99
Returns a value multiplied by 10n (and if necessary, round it up), where n is the number of 9's after the V.
So you can do:
select to_char(18.45, '000000000000V00000') from dual;
TO_CHAR(18.45,'000000000000V00000')
-----------------------------------
00000000001845000
or without the leading space (which is a placehold for a minus sign in case there are negative values):
select to_char(18.45, 'FM000000000000V00000') from dual;
TO_CHAR(18.45,'FM000000000000V00000')
-------------------------------------
00000000001845000
db<>fiddle
Also you can multiply for 100000 the given number:
SELECT TO_CHAR(18.45 * 100000, '00000000000000000') FROM DUAL;
This should do it:
SELECT REPLACE(TO_CHAR(18.45, 'FM000000000000D00000', 'NLS_NUMERIC_CHARACTERS=''.,'''), '.', '') FROM DUAL;
The NLS_NUMERIC_CHARACTERSmakes sure the decimal separator is a . regardless what the session is configured. This way we're safe to remove it from the resulting string with the replace function.
The FM is used to suppress the leading space character.

Use Convert() to add two digits the end of a number and a comma to the left?

I'm asked to use the CONVERT function to return the third column as a datatype that outputs 2 digits to the right of the decimal point and all comma’s to the left (i.e. 3, 106.34). Name it FormatTotal.
This is where I'm at.
USE AP
SELECT InvoiceTotal,
CAST(InvoiceTotal AS int) intTotal,
CAST(InvoiceTotal AS decimal(8,1)) DecimalTotal
CONVERT(decimal(???), InvoiceTotal) AS FormatTotal
FROM Invoices;
This looks like a task for format():
format(InvoiceTotal, 'N2', 'en-US') as FormatTotal
N is the format specifier for numbers.
2 gives you the precision (ie the number of decimal digits).
en-US defines the comma separator for thousands and the dot separator for decimals.

Formatting a string to 2 decimal places?

I have a number that looks like this:
'0000040000'
How can I turn it into a string that looks like this:
400.00
This should work on numbers like this as well :
1234540067 -> 12345400.67
I suspect it's fair to say you have a string that you would like to format as a number.
If you want to "learn" how to do this, I suggest looking up the convert, cast and format functions for SQL Server and gain some extra knowledge.
I've elected to first convert to a numeric type, divide by 100 and format the output.
This saves the need to trim leading zero's.
select format(convert(numeric(18,2), '0000040001') / 100, '0.00'))
Following SQL expression will first change string to decimal and then convert it back to a string in a required format:
SELECT FORMAT(CAST ('0000040000' AS DECIMAL(12,2))/100,'#.00')
This should work:
select convert(varchar(10), convert(decimal(10, 2), try_convert(int, '0000040000') / 100.0))
Why do you want the value as a string? Does a decimal work for your purposes?
Another method is:
select ltrim(str(try_convert(int, '0000040000') / 100.0, 10, 2))
Or using just string manipulations:
select replace(ltrim(replace(stuff('0000040000', 9, 0, '.'), '0', ' ')), ' ', '0')
This would be more elegant if ltrim() in SQL Server accepted the character to trim (as most other databases allow).
Here are a some possible solutions:
DECLARE #x CHAR(10)='0000040000'
SELECT CONVERT(VARCHAR(11),CONVERT(NUMERIC(10,2),CONVERT(NUMERIC(10),#x)/100))
SELECT CONVERT(VARCHAR(11),CONVERT(NUMERIC(10,2),STUFF(#x,9,0,'.')))
Simple as this:
SELECT ROUND('0000040004', 2, 1)/100;
Tested this with SQL Server 2017. It does not care about the fact that the number is a string, it does math on it just fine. Result of the above is 400.04
Your query is more of mathematical one.
To get the last 2 numbers after decimals, get the remainder of the number by dividing it by 100.
To get the digits, leaving the last 2 digits, divide the number by 100 again.
select convert(varchar,1234540067/100)+'.'+ convert(varchar,1234540067% 100);

Adding trailing and leading zeroes

How do we convert and add trailing and leading zeros to a number? For example 123.45. I need to make this ten digits long and have padding numbers in front and back. I would like to convert it to 0001234500. Two trailing numbers after the last digit of the decimal. Remove the decimal. Fill in the remaining space with zeroes for the leading end.
I have this so far and it adds trailing zeroes and removes the decimal.
REPLACE(RIGHT('0'+CAST(rtrim(convert(char(10),convert(decimal(10,4),Field))) AS VARCHAR(10)),10),'.','') as New_Field
In MySQL you would have RPAD and LPAD to get stuff like this done, in SQL Server (2012+) you can get something similar by working with FORMAT.
Easiest way is to FORMAT your numbers with a dot so that they take the right place in the format string, then remove that dot. You need to specify a locale, since in different regions you will get a different decimal sign (even if you use . within the format pattern, you would get , in various locales) - using en-US makes sure you get a dot.
REPLACE(FORMAT(somenumber, '000000.0000', 'en-US'), '.', '')
A few examples:
WITH TempTable(somenumber) AS (
SELECT 3
UNION SELECT 3.4
UNION SELECT 3.45
UNION SELECT 23.45
UNION SELECT 123.45
)
SELECT
somenumber,
REPLACE(FORMAT(somenumber, '000000.0000', 'en-US'), '.', '')
FROM
TempTable;
Gives
3.00 0000030000
3.40 0000034000
3.45 0000034500
23.45 0000234500
123.45 0001234500
You seem to really be overthinking what you need to do here. If we take it in steps, perhaps you'll see that this can be achieved much more easily. This solution runs under the idea that the value 123.45 becomes 0001234500 and 6.5 becomes 0000065000.
Firstly, let's pad out the right hand side of the number 123.45 so that we have 1234500 That's easy enough : 123. 45 * 100 = 12345 So, to get 1234500 we simply need to multiple it by a couple of extra factors of 10:
SELECT 123.45 * 10000; --1234500.00
Ok, now, let's get rid of those decimal places. Easiest way, convert it to an int:
SELECT CONVERT(int, 123.45 * 10000); --1234500
Nice! Now, the finalstep, the leading 0's. A numerical value, in SQL Server, won't display leading zeros. SELECT 01, 001.00; Will return 1 and 1.00 respectively. A varchar however, will though (as it's not a number). We can, therefore, make use of that with a further conversion, and then then use of RIGHT:
SELECT RIGHT('0000000000' + CONVERT(varchar(10),CONVERT(int,123.45 * 10000)),10);
Now you have the value you want '0001234500'.
If you're only after padding, (so 6.5 becomes 0006500) then you should be able to work out how to achieve this with the help above (hint you don't need RIGHT).
Any questions, please do ask.

How to conver number of size N to string - oracle

The below query is giving me out put in correct format
select to_char(999.00,'9,99.00') from dual
output = 9,99.00
what if I don't have a specific size to the number?
999999999999999999999999999999999.00
should be converted as
9999999999999999999999999999999,99.00
Will it be possible to do that?
Is there any formats for that ?
use the FM prefix in your formatting string. It trims leading spaces from the result of the conversion:
select ''''||to_char(123456.00, 'FM999999999999999999,99.00')||''''
, ''''||to_char(123456.00, '999999999999999999,99.00')||''''
from dual
;
produces
'1234,56.00'
' 1234,56.00'
Note:
You have to specify as many digits as the largest number to be converted will have in its shortest representation matching the format. Turning a complicated phrasing into a simple example, to_char(123456.00, 'FM9,99.00') won't work.