Oracle number to char leading 0 - sql

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

Related

How to get first 4 digits in a number, with oracle sql

I could do
select substr(to_char(20041111), 1, 4) FROM dual;
2004
Is there a way without converting to string first?
You can use the FLOOR function:
select floor(20041111/10000) from dual;
The following does not convert to a string but I'm not sure it's more readable...:
select floor(20041111 / power(10, floor(log(10, 20041111) - 3)))
from dual;
log(10, 20041111) -> 8.3... meaning that 10 ^ 8.3... = 20041111
if you floor this value, you get the number of digits in the base 10 representation of your number
if you want to remove digits, you just need to divide by 10 ^ (#digits - 3) (and not -4 since 10^1 already has 2 digits)
Another approach would be to use REGEXP_REPLACE
SELECT REGEXP_REPLACE(20041111,'....$') FROM dual;

Round Function in SQl

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;

Removing zero from decmial

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

Round to the nearest odd integer in SQL

I've found myself somewhat improbably* needing to round to the nearest ODD integer in SQL. There's a nice solution for how to round to the nearest N (2,5,10,etc) here, but nothing explicitly on odd numbers. Using Oracle 11gR2, if there are solutions particular to Oracle out there.
*Need to join my data to tables stripped from this study. The authors used a consistent bin width of 2...but sometimes it's even, and others it's odd.
You could do something like this:
DECLARE
n FLOAT;
BEGIN
n := 195.8;
SELECT
CASE
WHEN mod(FLOOR(n),2) = 0 THEN FLOOR(n)+1
ELSE FLOOR(n)
END NUM
INTO n
FROM DUAL;
dbms_output.put_line(to_char(n));
END;
/
Sometimes straightfoward is best, as people who come along after you will understand what's going on.
I don't think you need a case statement, this should do it:
SELECT
ROUND((11.9-1)/2,0)*2+1
FROM DUAL
Here is an oracle PL/SQL Function that would do that:
CREATE OR REPLACE FUNCTION ROUNDODD
(
IMPNUM IN NUMBER
) RETURN NUMBER AS
roundnum number;
oddnum number;
BEGIN
roundnum := round (IMPNUM,0);
IF mod(roundnum,2) = 1
THEN RETURN roundnum;
ELSE
IF roundnum > IMPNUM
THEN RETURN roundnum-1;
ELSE RETURN roundnum+1;
end if;
end if;
END ROUNDODD;
Arithmetic OR?
ROUND(3.14,0)|1
EDIT
Andriy correctly corrects this to FLOOR(3.64)::int|1. (which works correctly).
using modulus can help you find even/odd numbers. add 1 to the even numbers
select
case when (value % 2) <> 0 then value
else value + 1 end
from table

Decimal number, to_char, and Oracle

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