oracle 10g , query format - oracle

I have a small doubt. I have below query
SELECT empno
|| '|'
|| ename
|| '|'
|| sal
|| '|'
|| comm
FROM (SELECT empno,
ename,
sal,
comm
FROM emp);
the output is coming as :
7611|Grp Fract|2001|.11
7499|ALLEN WOR|1600|.22
7521|WARD|1250|10.23
7566|JONES|2975|234.23
7654|MARTIN|1250|.98
the last column COMM has value as 0.11, 0.22, 0.98
but the above query returns data as .11,.22,.98. Can anyone help me understanding why it is happening when I am concatenating the data and how to resolve this , I need exact value as it is in COMM column.
The o/p should be as
7611|Grp Fract|2001|0.11
7499|ALLEN WOR|1600|0.22
7521|WARD|1250|10.23
7566|JONES|2975|234.23
7654|MARTIN|1250|0.98
Ths comm column is defined as number(7,2).
Thanks

Use the TO_CHAR function with a proper format model. It seems you want LTRIM(TO_CHAR(comm,'999990.99')) here.

The LTRIM(TO_CHAR(comm,'999990.99')) works for values which have 2 digits after decimal point.
If value like 0.123523 is used above it rounds off the decimal places keeping 2 digits only.

Related

How to replace positions in a number by special characters in oracle?

My table contain column name accountnumber;
acctnum
-------
1234556777
2335678000
i want an output using oracle using sql query
acctnum
---------
123xxx6777
233xxx8000
i tried replace function ,but i did not get the output.pls help.
If I did not understand wrong, This maybe help you?
with a as
(select '1234567' as acctnum from dual)
select '###' || substr(acctnum,4) from a;
If you're trying to replace characters 4, 5 and 6 with an x you can do it with the SUBSTR function like this:
SELECT SUBSTR(acctnum, 1, 3) || 'xxx' || SUBSTR(acctnum, 7) AS acctnum
FROM mytable;

Oracle date time to datetime

I've looked at the answers on here but none of them seem to work.
I have the following date and time columns with example times how they are stored as below:
DATE_V TIME_V
26-NOV-15 10:58
How do I add these together into one column and convert it to a datetime as below? The trailing zeros are not necessary.
DateTime_V
2015-11-26 10:58:00.000
I’ve used the following which saves it as string but I can't get it to datetime.
TO_CHAR(DATE_V, 'YYYY-MM-DD') || ' ' || TO_CHAR(TO_timestamp(time_V, 'HH24:MI'),'HH24:MI')
= 2015-11-26 10:58
Assuming (bad word) that these are both stored as VARCHAR2 fields the following should work:
SELECT DATE_V, TIME_V, TO_DATE(DATE_V || ' ' || TIME_V, 'DD-MON-RR HH24:MI') AS DATETIME_V
FROM YOURTABLE
SQLFiddle here
Best of luck.

Oracle 11.2 to_number multiple commas

In Oracle 11.2, is there some number format, nf, that will work with to_number to parse arbitrary length varchar2s containing digits and commas?
I can achieve this without a number format, by using regexp_replace, but I'd prefer to achieve the same thing using just a number format.
e.g., the following 2 statements work:
select to_number(regexp_replace('12,345', ',', '')) from dual;
select to_number(regexp_replace('1,234,567', ',', '')) from dual;
but I'd prefer:
select to_number('12,345', nf) from dual;
select to_number('1,234,567', nf) from dual;
where nf is one number format string that works for both statements.
If I try nf = '99,999', the first statement works, but the second fails.
Thanks.
Oracle won't complain if the number format is too long, so you can use a model that has enough digits to cope with the biggest number you can receive:
SQL> select to_number('12,345',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
TO_NUMBER('12,345','999G999G999G999G999G999G999G999G999G999G999G999G999')
-------------------------------------------------------------------------
12345
SQL> select to_number('1,234,567',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
TO_NUMBER('1,234,567','999G999G999G999G999G999G999G999G999G999G999G999G999')
----------------------------------------------------------------------------
1234567
SQL> select to_number('999,999,999,999,999,999,999,999,999,999,999,999,999',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
TO_NUMBER('999,999,999,999,999,999,999,999,999,999,999,999,999','999G999G999G999
--------------------------------------------------------------------------------
1.0000E+39
I've used the G group separator instead of a fixed comma to support globalisation, but the effect is the same.
The only caveat is that the source number has to have the right grouping so it matches the formatting exactly for the digits it does have:
SQL> select to_number('1,2345',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
select to_number('1,2345',
*
ERROR at line 1:
ORA-01722: invalid number
Although I support Alex Poole's answer, here's another crude but effective way of solving the problem that should perform better than doing a regex.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_of_numbers (
example_num VARCHAR2(50)
)
/
INSERT INTO table_of_numbers (example_num)
VALUES ('12,345')
/
INSERT INTO table_of_numbers (example_num)
VALUES ('1,234,567')
/
Query 1:
SELECT TO_NUMBER(example_num, RPAD('9', LENGTH(example_num) - 1, '9')) fudge
FROM table_of_numbers
Results:
| FUDGE |
-----------
| 12345 |
| 1234567 |
If you need to match the commas, then you could do something slightly more sophisticated with INSTR and LPAD to make sure you generate the right mask.
For this :
select to_number('1,234,567', nf) from dual;
Use nf = 9,999,999 will work.

Sequence and case in a select in oracle

I'm trying to do this query (in oracle) but I have some problems:
SELECT CASE
WHEN deptno = '10' THEN scott.seq.nextval
|| 'next10'
WHEN deptno = '20' THEN scott.seqnextval
|| 'next20'
WHEN deptno = '30' THEN scott.seq.currval
|| 'curr'
END col_1
FROM scott.emp;
I'm getting this results:
COL_1
----------------------------------------------
191next20
192curr
193curr
194next20
195curr
196curr
197next10
198next20
199next10
200curr
201next20
202curr
203next20
204next10
205next20
206next10
207next10
And this is what I think they should be:
COL_1
----------------------------------------------
191next20
192curr
193curr
194next20
194curr
194curr
197next10
198next20
199next10
199curr
201next20
201curr
203next20
204next10
205next20
206next10
207next10
So, why i get the next value of the sequence also when I should have the current value and not only when the case selects the next value?
Yeah, this could be done with a plsql script but I can't.
Thanks you!
Nextval and currval are not functions, but are "Sequence Pseudocolumns".
"Within a single SQL statement containing a reference to NEXTVAL, Oracle increments the sequence once: For each row returned by the outer query block of a SELECT statement. Such a query block can appear in the following places. ..." (emphasis added) [Oracle Database SQL Language Reference, "How to Use Sequence Values"]
In other words, seq.nextval is not a function with a side affect, but a pseudocolumn that has a particular value per row. Once there is a single reference to seq.nextval, the value increments for every row, whether or not the value is used. The outcome OP is seeing is a peculiar to sequences, not case expressions. For example, same thing with decode:
SQL> select decode(deptno
2 , 10, seq.nextval || 'next10'
3 , 20, seq.nextval || 'next20'
4 , 30, seq.currval || 'curr30')
5 from emp;
DECODE(DEPTNO,10,SEQ.NEXTVAL||'NEXT10',20,SEQ.
----------------------------------------------
35next20
36curr30
37curr30
38next20
39curr30
40curr30
41next10
42next20
43next10
44curr30
45next20
46curr30
47next20
48next10
Interesting. Per the Oracle docs:
The statements in a WHEN clause can modify the database and call
non-deterministic functions. There is no fall-through mechanism as in
the C switch statement
Notice it doesn't say the statements in the "true" WHEN clause. So even if the when statement is false, the nextval will fire:
select
case when 1=0 then 'next ' || seq_id.nextval
when 1=1 then 'curr ' || seq_id.currval
end col1
from dual;
I must admit this is different than I expected.
EDIT:
See answer from ShannonSeverance

Oracle appending string to a select statement

I have a column in a oracle table Lic_num char(7 byte)
SELECT column1, 'ABC' + Lic_num
FROM TABLE One
I wanted ABC appended to all the rows that are returned with lic_num
appended to it.
I tried tha above query and it is not working.
In Oracle it's:
SELECT column1, 'ABC' || Lic_num
FROM TABLE_ONE
This would be the way of doing it.
SELECT column1, 'ABC' || Lic_num FROM TABLE_ONE;
SELECT CONCAT(CONCAT(column1, 'ABC'), Lic_num) FROM TABLE_ONE;
If you need you can rename the concatenated Column name using AS keyword so it would be meaningful in terms of reporting.
Below info is included to help someone looking at concatenation in detail.
There are two ways to concatenate Strings in Oracle SQL. Either using CONCAT function or || operator.
CONCAT function allows you to concatenate two strings together
SELECT CONCAT( string1, string2 ) FROM dual;
Since CONCAT function will only allow you to concatenate two values together. If you want to concatenate more values than two, you can nest multiple CONCAT function calls.
SELECT CONCAT(CONCAT('A', 'B'),'C') FROM dual;
An alternative to using the CONCAT function would be to use the || operator
SELECT 'My Name' || 'My Age' FROM dual;

Resources