Just help me out to display a number whose length is unknown with 2 decimal places.
Example:
If the number is 230000 then i need it to be printed as 230000.00
I tried
Select to_char(amount, 'FM99999999.90') as amt from customer;
Formatting is usually front-end "problem"; every decent tool (be it Oracle Apex, Reports, Forms, Jasper Reports, ...) has an option to format a number as you want it. Why? So that those values remain numbers, to be able to apply e.g. SUM function to them or whatever you may need.
In SQL*Plus (or similar, even GUI tools), formatting is done by the TO_CHAR function and desired format mask. If you want two decimals and (just for example) thousands separator, you might do something like this:
SQL> with customer (amount) as
2 (select 230000 from dual union all
3 select 3.14 from dual union all
4 select 0.0002 from dual union all
5 select 25.123 from dual
6 )
7 select amount,
8 to_char(amount, 'fm999g999g990d00') new_amount,
9 lpad(to_char(amount, 'fm999g999g990d00'), 10, ' ') new_amount2
10 from customer;
AMOUNT NEW_AMOUNT NEW_AMOUNT2
---------- --------------- ----------------------------------------
230000 230.000,00 230.000,00
3,14 3,14 3,14
,0002 0,00 0,00
25,123 25,12 25,12
SQL>
note that new values have ...0d00 format mask which makes sure you'll actually see zeroes around the decimal point.
use G and D for thousand groups and decimals, rather than commas and dots
new_amount2, aditionaly, has lpad applied so that values are right-aligned. I presumed that max length of those values is 10 (you'd know better)
If you do use SQL*Plus (which is quite rare nowadays), you could even use its set numformat command so - no additional modifications are necessary; you just select what you have:
SQL> set numformat 999g990d00
SQL>
SQL> with customer (amount) as
2 (select 230000 from dual union all
3 select 3.14 from dual union all
4 select 0.0002 from dual union all
5 select 25.123 from dual
6 )
7 select amount
8 from customer;
AMOUNT
-----------
230.000,00
3,14
0,00
25,12
SQL>
Use concat as follows:
select concat(amount, '.00') from customer
Related
How to calculate grand total in query in SQL developer in oracle.
Break on report
Compute Sum is not working in SQL developer.It works only in SQL plus.
Don't want to use group function also.
Is there any way to run a query in SQL developer and show the total at the end of the results/rows.
Also want to use it in jdbc java the query
For Example , i need to select a table and list all records matching certain criteria and show the sum at the end of the records
Select volume, value , *.t1 from table1 t1 where trunc(create_date) = trunc(sysdate)
This will display say 50 records..i want to display at the end of all records under column volume and value ,the grand total of these 2 columns.
Volume value xxx yy zzzz
25 123.5 aa bb cc
35 10 a c b
50 100 c nn xc
-- --
110 233.5
You can use the below query to achieve the result -
SELECT volume,
value ,
t1.*
FROM table1 t1
WHERE TRUNC(create_date) = TRUNC(SYSDATE)
UNION ALL
SELECT SUM(volume),
SUUM(value),
NULL,
NULL,
... -- Add other NULLS according to your column list in table1.
FROM table1;
"I don't want" is a poor reason.
How come you can & want to use SQL*Plus' commands (which won't work elsewhere), but standard SQL capabilities are out of your sight? I'd suggest you to reconsider what you said & thought.
If you do, rollup might be a nice option for you.
SQL> select deptno,
2 sum(sal) sum_sal,
3 sum(comm) sum_comm
4 from emp
5 group by rollup(deptno);
DEPTNO SUM_SAL SUM_COMM
---------- ---------- ----------
10 8750
20 10875
30 9400 2200
29025 2200
SQL>
You can use ROLLUP FUNCTION here.
SELECT SUM (volume) volume,
SUM (value) value,
xxx
FROM table1 t1
WHERE TRUNC (create_date) = TRUNC (SYSDATE)
GROUP BY ROLLUP (xxx)
Column amount contains data like - 200.203, 200, 5.10, 45.20, 10 and 5000213.012
Now I want to select if the number does not include any decimal digit then we add .00 in the returning result.
Expected result - 200.203, 200.00 ,5.10, 45.20 , 10.00 and 5000213.012
You can use TO_CHAR to format the value so that it has at least 2 decimal places:
SELECT value,
TO_CHAR( value, 'FM9999999990.009' ) AS formatted_value
FROM table_name
Which, for the sample data:
CREATE TABLE table_name ( value ) AS
SELECT 200.203 FROM DUAL UNION ALL
SELECT 200 FROM DUAL UNION ALL
SELECT 5.10 FROM DUAL UNION ALL
SELECT 45.20 FROM DUAL UNION ALL
SELECT 10 FROM DUAL UNION ALL
SELECT 5000213.012 FROM DUAL;
Outputs:
VALUE | FORMATTED_VALUE
----------: | :--------------
200.203 | 200.203
200 | 200.00
5.1 | 5.10
45.2 | 45.20
10 | 10.00
5000213.012 | 5000213.012
db<>fiddle here
Following some comments, the OP's question is effectively:
How can I change this number to have a given format?
When you consider that a NUMBER data type has no format and 5, 5.0 and 5.00000 are exactly the same value and the database does not store how many trailing zeroes a decimal value has then this question does not entirely make sense as there is no way to give a number a format.
Instead the question can be formulated as either:
How can I get <insert name of client program> to display numbers so that they always have at least 2 decimal places?
We can't answer this question without knowing the client program so I'll skip it.
Or:
How can I display a number from an Oracle query so that it is formatted with at least 2 decimal places?
Since NUMBERs never have any formatting of their own then this must involve a conversion to another data-type which can represent the number with a format (i.e. a string). So the result is that if you want a NUMBER data type then Oracle will not give it a format (but the client program might) but if you want to change the NUMBER to a string data type then you can give it a format (but it won't be a NUMBER as its now a string that just happens to contain digits) and, as described above, TO_CHAR does that.
This will do:
select to_number(to_char(value, '9999999.99')) from dual;
I have a table DATA_TABLE, it have MAX_RATE_UNIT column as float value.
Below is the table and MAX_RATE_UNIT column values like,
DATA_TABLE
----------
MAX_RATE_UNIT
-----------
1.2
1
3
3.4
0.445
2343.43434
2.123
0.3433423
I want a select query which has return the result as minimum one decimal point if the decimal is not have in tha column value, as 1--> 1.0 and 3-> 3.0 and remaing are same value to return,
OUTPUT:
MAX_RATE_UNIT
-------------
1.2
**1.0**
**3.0**
3.4
0.445
2343.43434
2.123
0.3433423
Help me for this logic,
Convert the number to a string explicitly with to_char() and a suitable format model:
-- CTE for sample data
with data_table (max_rate_unit) as (
select 1.2 from dual
union all select 1 from dual
union all select 3 from dual
union all select 3.4 from dual
union all select 0.445 from dual
union all select 2343.43434 from dual
union all select 2.123 from dual
union all select 0.3433423 from dual
)
-- actual query
select to_char(max_rate_unit, 'FM999999990.099999999') as max_rate_unit
from data_table;
MAX_RATE_UNIT
--------------------
1.2
1.0
3.0
3.4
0.445
2343.43434
2.123
0.3433423
Strategically using 0 instead of 9 immediately before and after the decimal point means you get a leading zero for values < 1, and at least one digit after the decimal point; the FM modifier stops that showing all the trailing digits.
You need enough placeholders in toe format model, either side of the decimal point, to account for the full range of values you might have to handle. (My original edit didn't have enough so the last digit of the last value was silently truncated...)
I have a string format of HISTORY-VDF-DE-EOF-WORK VDF-DE-EOF-WORK FFOR-1!20170904T105949.630 GMT in a column.
Need to extract FFOR-* from it.
where FFOR-* is ascending order and 20170904T105949.630 is date format and timestamp which changes for every entry in column.
Have tried
SELECT REGEXP_SUBSTR('source column', '\d[FFOR-]*') as order FROM table;
but no luck.
How can I extract it.
Thanks in advance
Does it have to be a regular expression solution? Because, an ordinary SUBSTR with INSTR does the job as well:
[EDITED, after seeing what output is required]
SQL> with test as
2 (select 'HISTORY-VDF-DE-EOF-WORK VDF-DE-EOF-WORK FFOR-1!20170904T105949.630 GMT' col
3 from dual
4 )
5 select
6 substr(col,
7 instr(col, 'FFOR-'),
8 instr(col, '!', -1) - instr(col, 'FFOR-')
9 ) result
10 from test;
RESULT
------
FFOR-1
SQL>
[EDIT #2, with REGEXP]
Note that, although it looks "smarter", for large data volume it might be slower than SUBSTR option.
SQL> with test as
2 (select 'HISTORY-VDF-DE-EOF-WORK VDF-DE-EOF-WORK FFOR-1!20170904T105949.630 GMT' col
3 from dual
4 )
5 select regexp_substr(col, 'FFOR-\d+') result
6 from test;
RESULT
------
FFOR-1
SQL>
I started working in BI and I was given a brain teaser since I came from C# and not SQL/cognus.
I get a number. It can be between 0 and a very large number. When I get it and it's below 1,000 everything is dandy. But if it's bigger than or equal to 1,000 , I should use 1,000 instead.
I am not allowed to use conditions, I need it to be pure math, or if I can't then I should use efficient methods.
I thought it would be easy and just use Min() but that works differently in cognus and SQL apparently.
Use the LEAST() function:
Oracle Setup:
CREATE TABLE data ( value ) AS
SELECT 1 FROM DUAL UNION ALL
SELECT 999 FROM DUAL UNION ALL
SELECT 1000 FROM DUAL UNION ALL
SELECT 1001 FROM DUAL;
Query:
SELECT value, LEAST( value, 1000 ) AS output FROM data
Output:
VALUE OUTPUT
----- ------
1 1
999 999
1000 1000
1001 1000