Question

There is a "INV_NM" Column as below in my Table INVENTORY

1000--200
10000--2000
900--100
2000--2200
70000--2100
700--700
4000--4000
2000--2000
500--1000
8000--750
6000--2000
100--1000

Its Synonym of DEPT--DIVISION

I want to fetch in my query only the DEPT as DEPT_NO in one column and DIVISION as DIVISION_NO in another column.

I can use substr(INV_NM,1,X)

But X is my confusion

Please help me on this regard

Was it helpful?

Solution

You could achieve the desired output with the following combination of substr() and instr() functions:

select substr(inv_nm, 1, instr(inv_nm, '-')-1)    as dept_no
     , substr(inv_nm, instr(inv_nm, '-', -1) + 1) as DIVISION_NO
  from inventory

Result:

DEPT_NO     DIVISION_NO
----------- -----------
1000        200         
10000       2000        
900         100         
2000        2200        
70000       2100        
700         700         
4000        4000        
2000        2000        
500         1000        
8000        750         
6000        2000        
100         1000        

12 rows selected 

SQLFiddle Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top