i could not understand why following code
SQL>
Select to_number('1234.64', '9999.9') from Dual;
returns this number 1234.6?is it something like rounding ,truncation or?please help me to understand this code,i know to_number functions,i have used many times this code for simple chars,but here it is not clear anything
This looks a lot like Oracle, but I suspect that the result would be similar in any SQL that used to_number.
The to_number function takes two arguments: the string to be converted to a number, and the format string for the conversion.
In the example, '12345.64' is the string to be converted, while '9999.9' is the format string. In this format string, a 9 stands for a digit while a . stands for the decimal point.
So the function is asking to convert the string '12345.64' to a number with up to 4 digits to the right of the decimal point, and only 1 digit after the decimal point.
The second argument is optional - under normal circumstances, I would omit it.
You should use
SELECT to_number('1234.64', '9999.99') from Dual;
Your mask tells engine you want just one decimal, so number gets rounded.
If you want to get exact number, don't specify any mask:
SELECT to_number('1234.64') from Dual;
Related
Here's what i'm having trouble with. I have a table where i have the column weight, the data in this column is varchar2, example: 60 kg.
When i try to make a WHERE condition asking to retrieve everything below 60 kg, it doesn't give me the wanted results.
I tried to convert using to_number but it's not working and giving me error "Invalid Number". I also tried to cut the kg with SUBSTR and convert, and doesn't work as well.
Any suggestions?
Thanks in advance.
You can extract leading digits and convert that to a number:
select to_number(regexp_substr(weight, '^[0-9]*'))
from t;
The regular expression is starting from the beginning of the string (the ^) and extracting digits. The * is for any number of digits. Regular expression match is "greedy" by default, meaning that it will keep matching as many digits in a row as there are.
If you have decimal places or negative values, this might be a tad bit more complicated.
I have this number 0101110 but i need only 5 digits. for exemple (01110)
i used this
select to_char(trim('0101110')::int,'99999V')
but PG returns #####
if someone can help me , thanks a lot.
How do you have a number that starts with a zero? I suspect your "number" is really being stored as a string.
If so, just use right() to take the rightmost five characters:
select right(col, 5)
If the value is really stored as a number, then convert it to a string first.
I need to convert numbers in text format. It could be integers and decimal numbers.
If convert integer without regexp_replace like
select to_char(999,'FM9999.9') from dual;
999.
Solution for this problem is
regexp_replace(to_char(999.9,'FM9999.9'),'[.]$')
Are there any other way to do it without regexp_replace ?
You can use the trim or rtrim functions:
select to_char(999,'FM9999.9') as plain,
rtrim(to_char(999,'FM9999.9'),'.') as trimmed
from dual;
PLAIN TRIMMED
------- -------
999. 999
Documentation for the rtrim function.
You can't do it within the to_char() call though, if that is what you were hoping for; there isn't a format model element that makes the decimal separator optional depending on whether there are decimal digits.
I have a column in my table which showing an amount. The amount is varying from one column to another and they are more than 15 digits.
What is the best way to format the number to show commas and decimal points?
My query is
select amount from ccamounts
How can I format the number
205511892078
to show as
205,511,892,078
and if there is a radix point it will also appear.
I believe you can use TO_CHAR to do this, the issue is that this is just a formatting function within SQL. It requires that your number is always going to be in the same format.
taking the example above you could do
TO_CHAR('205511892078', '999,999,999,999')
and this would format the number as you have specified, with a decimal place this can be done aswell but the decimal needs to be specified:
TO_CHAR('20551189207842', '999,999,999,999.99')
which would give you 205,511,892,078.42
I think if the field length is going to vary sql will just ignore anything that doesn't fit into the format string (It's a mask). Perhaps you want to consider formatting the number in this case on whichever front end you may be using?
I would format the number in the UI / Reporting tool / Presentation layer not Oracle
but if you MUST format it in oracle try:
SELECT
CASE WHEN INSTR( TO_CHAR(205511892078),'.')>0 THEN
TO_CHAR(205511892078 ,'999,999,999,999.99')
ELSE
TO_CHAR(205511892078 ,'999,999,999,999')
END
FROM DUAL
this will return the number as a string.
declare #d3 decimal (10, 2)
set #d3 = 12309809.5494
SELECT convert(varchar(15),cast(CAST(ROUND(#d3,2,1) AS DECIMAL (30,2)) as money),1) as Value
SELECT CAST(ROUND(convert(varchar(30), cast(#d3 as money),2),2,1) AS DECIMAL (30,2)) as Value
Output:
12,309,809.55
12309809.55
I've been playing with cast()s and such with this and can't seem to get things to work. I have a varchar string that's 18 characters long that I'd like to convert or cast to a decimal, with five decimal places. So for instance, this string:
00000001987600130
Would become 19876.00130
It's the case that I'll always have a 17 character string, with the last five characters reserved as the decimal place.
I've been playing with casts and converts but I'm not quite there. For instance, these statements get me (sort of) close but not exactly.
select CAST('00000001987600130' as bigint)/100000.0
select (convert(decimal(17,5),left('00000001987600130',12),0))
If you have a suggestion I'm happy to try it. Thanks!
This works fine for me:
SELECT CONVERT (decimal, '00000001987600130') / 100000
The reason why the first one didnt work is because the result of the CAST is an integer, and dividing an integer by 100000 rounds / truncates it (not sure which) so that it is still an integer.
To ensure you get what you want do a final CAST to ensure decimal(17,5) exactly
SELECT CAST((CAST('00000001987600130' AS decimal) / 100000) AS decimal(17,5))
Otherwise, the output type is not correct in scale or precision and may have effects later.