I got a output like this
0.00234690616839645663803848176618444236941
the way I want is
2.3
first I remove the zero
select replace(0.00234690616839645663803848176618444236941,0) from dual;
then I try to do the round function on it , but its giving me zero any idea how can we get this
select round(replace(0.00234690616839645663803848176618444236941,0) ) from dual;
You can try this :
select round(0.00234690616839645663803848176618444236941 * 1000,1) from dual;
Result:
2.3
When you strip out the zeros, you are left with .23.....
Select round (.23, 0) will return 0, because you are telling the db to round to 0 decimal places.
If you multiply the result of your replace by 10, that will get you want you want. Not sure what you are doing makes any sense, but it works:
select round (10 * (replace(0.00234690616839645663803848176618444236941,0)),1) from dual;
SQL Fiddle
Related
I am running the below query in oracle.
WITH
ta AS (
SELECT account_coid
,txn_id
,cbdev.cbzdt(effective_date) AS effective_date
,cbdev.cbchr(utl_raw.substr(txn_data, 113, 20)) AS CESG_amt
FROM bs_transaction
WHERE sub_type = 127469880)
SELECT
cast(ta.CESG_amt as DECIMAL (20,2)) AS cesg_amt
from ta
inner join ....
Here, i m getting the result (cesg_amt) as -156.57. But i need the result as -0000000000156.57.
I need the leading zeros with - retained (leading 0's and also the two digits after the decimal).
I have tried as to_char(ta.CESG_amt, '0000000000000.00') AS cesg_amt in the query but of no use.
Can you please help me what needs to be done in the DECIMAL field to get the result as below.
You may use such a formatting :
select to_char(-156.57,'fm0000000000000D00','NLS_NUMERIC_CHARACTERS = ''.,''')
as Result
from dual;
RESULT
-----------------
-0000000000156.57
select to_char(-156.57,'000000000.00')
from dual;
I have a function that calculates number of days
select (x * 0,5) from dual.
my problem is tha when x=1 i get .5 instead of 0,5.What i want to get is numbers in format 0.5,1,1.5,2,2.5 ..
Could you please give me a help with this.
I know this is old but I did the following, where x is datatype NUMBER(20,3)
CASE WHEN x < 1 THEN '0'
ELSE '' END
|| to_char(x)
I didn't want any trailing zeros but did want a leading zero if x was, say, 0.99 or 0.5
So I'm just using the regular to_char and adding a leading zero if x is less than 1.
Try to use:
select TO_CHAR(x * 0,5, '990,99') from dual
EDIT:
Try to create a function like this:
create function getDecimalFormat(x in number)
return varchar2
as
begin
return RTRIM(TO_CHAR(x, 'FM999999999999990.99'), '.');
end;
If you always want 1 decimal place use to_char to round
select TO_CHAR(28.324, '90.9') from dual;
returns
28.3
and
select TO_CHAR(28, '90.9') from dual;
returns
28.0
Hope this helps
select Round((10*100)/115,0)
Result : 8
But Result : 8.69
But i want to display 9
I tried below query also, but result is same..
select Round((10*100)/115,0)
Please solve my prob....
Thanks
Try below
declare #n decimal(4,1)
select #n = 8.69
select case when PARSENAME(#n,1)>=5 then ceiling(#n) else floor(#n) end
idea is if the number after point is greater than 5 then go to upper value else go to lower one
If your database is oracle then the following function you can use..
For result 9 use ceil function and For result 8 use floor function
select ceil(8.69) val from dual;
select floor(8.69) val from dual;
for upper value you can use Ceil function and for lower value you can use floor function in oracle as below
select ceil(8.69) Upper_val,floor(8.69) Lower_val from dual;
It returns an integer. Try to convert it to a decimal before calculation
Eg.
select Round((10*100)/115,0)
change to
select Round((10.0*100)/115,0)
Hope this will help
Here is what's happening:
Even before using ROUND Function
SELECT 1000/115 /* the result is 8 */
and try this
SELECT ROUND(1000.0/15, 0) /* the result is 9.000000 */
and if use this
SELECT CEILING(1000.0/115) /* the result is 9 */
so basically before call any function the result is 8.
you can use this:
SELECT ROUND(CONVERT(decimal, 1000) / 115, 0)
or simply this:
SELECT ROUND(1000.0/15, 0)
If Its SQL Server please do try this:
SELECT CEILING(10.1) AS UpperValue, FLOOR(10.6) AS LowerValue,
Round(CAST((CAST(10 AS float) * CAST(100 AS float)) / CAST(115 AS float) AS float), 0)
AS YourValue, Round((10*100)/115,0) AS YourOldValue;
I want to get a number 5000.1 and divide it by 1000 before adding an "F" infront of it.
How do i do this? I tried and failed this:
select "F" + round ( acq.store_size_net / 1000, 0) from acq
I suspect your missing the cast of the number to a text data type
Without knowing the exact dialect of sql you're using im gonna hazard a guess at ms-sql
select 'F' + cast(cast(round ( 5000.1 / 1000, 0)as int) as nvarchar(50))
produces output
F5
This will work in Oracle :
select 'F' || round (acq.store_size_net / 1000, 0) from acq
I am trying to figure out a format spec of to_char() that would give me the following result.
to_char(0.1, '[FORMAT_SPEC]')
gives 0.1 and:
to_char(1, '[FORMAT_SPEC]')
gives 1.
I've tried the following solutions:
to_char(0.1)
gives '.1'.
to_char(0.1, 'FM0.099')
gives 0.1, which is okay, however:
to_char(1, 'FM0.099')
gives 1.0, which is not okay.
Do you have any suggestions?
The precision returned needs to be consistent, so the only alternative is to use DECODE or CASE statements to conditionally return what you need:
CASE
WHEN INSTR(TO_CHAR(t.col), '.') = 0 THEN TO_CHAR(t.col)
ELSE TO_CHAR(t.col, 'FM0.099')
END
The example isn't great - it's not clear if your data will have values like 1.000 or values above one/etc.
EDIT Michael-O (2013-06-25): For those who need it idiot-proof, you may try:
case
when instr(to_char(<col>), (select to_char(0, 'FMD') from dual)) = 0
then to_char(<col>)
else to_char(<col>, 'FM999990D999')
end
It automatically observes the decimal separator. Adapt the the secodn format modal to your number size.
I just use this:
TRIM('.' FROM TO_CHAR(x, 'FM99990.999'))
Don't happen to have an Oracle instance handy to test this in, but I'd think that
TO_CHAR(1, 'FM0.999')
oughta do it.
Not sure what range of values you will be expecting but you could case out values < 1 versus those >= 1. Otherwise either the trailing 0 or the decimal is going to get in your way:
select val,
case when val < 1 then to_char(val, 'FM99990.9')
else to_char(val, 'FM99999')
end fmt
from (select 0.1 val from dual union all
select 1 from dual
)
/
VAL FMT
---------- --------
.1 0.1
1 1