How to extract a digit from number in oracle - sql

please help with query how to extract digit '1' from below table using SQL in oracle.
Table
1000
1001
1010
0100
expected result ;
Table
1
11
11
1

You can use the simple string function TRANSLATE (which is faster than regular expressions):
SELECT TRANSLATE(value, '10', '1') AS result
FROM table_name
If you have more than binary digits then:
SELECT TRANSLATE(value, '1023456789.', '1') AS result
FROM table_name
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT '1000' FROM DUAL UNION ALL
SELECT '1001' FROM DUAL UNION ALL
SELECT '1010' FROM DUAL UNION ALL
SELECT '0100' FROM DUAL;
Both output:
RESULT
1
11
11
1
db<>fiddle here

I expect you are giving us a simplified version of your problem? One way to achieve this is is using REGEXP_REPLACE to replace all characters but the character 1 with an empty space:
SELECT
REGEXP_REPLACE(YOUR_COLUMN,'[^1]','') AS DESIRED_RESULT
FROM YOUR_TABLE
You can check out this example: db<>fiddle

You can use regexp_replace:
WITH dat AS
(
SELECT '1000' AS numb FROM dual UNION ALL
SELECT '1001' FROM dual UNION ALL
SELECT '1010' FROM dual UNION ALL
SELECT '0100' FROM dual
)
SELECT regexp_replace(numb,'[^1]','')
FROM dat;

Related

Regex to get 00 if the string has less digits

I have a column that is giving me output like 'ABC2001' , 'ABC100145', 'ABC009282' ,' ABC1901'
I want to change this column value to have '00' in between literals and numbers if number is less than 6 digits. Something like -
COL_A
------------
ABC2001
ABC100145
ABC009282
ABC1901
Expected output
COL_B
------------
ABC002001
ABC100145
ABC009282
ABC001901
How to use regex for this ? Currently I am using
SELECT SUBSTR(COL_A,1,3)||LPAD(REGEXP_REPLACE(COL_A,'\D+'),6,'0') FROM TAB
and it is giving me output like -
ABC210073
ABC210073
You do not need (slow) regular expressions and can use simple string functions:
SELECT col_a,
SUBSTR(col_a, 1, 3) || LPAD(SUBSTR(col_a, 4), 6, '0') AS col_b
FROM table_name;
Which, for your sample data:
CREATE TABLE table_name (col_a) AS
SELECT 'ABC2001' FROM DUAL UNION ALL
SELECT 'ABC100145' FROM DUAL UNION ALL
SELECT 'ABC009282' FROM DUAL UNION ALL
SELECT 'ABC1901' FROM DUAL;
Outputs:
COL_A
COL_B
ABC2001
ABC002001
ABC100145
ABC100145
ABC009282
ABC009282
ABC1901
ABC001901
db<>fiddle here
Just for fun, for different prefixes:
usual string functions: trim/lpad/substr:
with table_name (col_a) AS (
SELECT 'ABC2001' FROM DUAL UNION ALL
SELECT 'ABC100145' FROM DUAL UNION ALL
SELECT 'ABC009282' FROM DUAL UNION ALL
SELECT 'ABC1901' FROM DUAL UNION ALL
-- other different prefixes:
select 'ABC2001' from dual union all
select 'AB100145' from dual union all
select 'A-BC9282' from dual union all
select 'A8C2374' from dual union all
select '7x-ABC32129' from dual union all
select '123ABC8942' from dual
)
select v.*, prefix||num as col_b
from (
select
col_a,
rtrim(col_a,'0123456789') as prefix,
lpad(substr(col_a,1+length(rtrim(col_a,'0123456789'))),6,'0') as num
from table_name
) v
;
DBFiddle
using regex functions:
with table_name (col_a) AS (
SELECT 'ABC2001' FROM DUAL UNION ALL
SELECT 'ABC100145' FROM DUAL UNION ALL
SELECT 'ABC009282' FROM DUAL UNION ALL
SELECT 'ABC1901' FROM DUAL UNION ALL
-- other different prefixes:
select 'ABC2001' from dual union all
select 'AB100145' from dual union all
select 'A-BC9282' from dual union all
select 'A8C2374' from dual union all
select '7x-ABC32129' from dual union all
select '123ABC8942' from dual
)
select
col_a,
regexp_replace(
regexp_replace(col_a,'(\d+)$','00000\1')
,'0*(\d{6})$'
,'\1'
) as col_b
from table_name
;
DBFiddle
regex solution for padding numbers to the maximum their length, ie not knowing max numbers length(if it's not hard-coded 6):
select
v.*,
regexp_replace(
regexp_replace(col_a,'(\d+)$',rpad('0',max_num_length,'0')||'\1')
,'0*(\d{'||max_num_length||'})$'
,'\1'
) as col_b
from (
select t.*, max(length(regexp_substr(col_a,'\d+$')))over() as max_num_length
from table_name t
) v
;
DBFiddle

how to get the missing values in SQL query when using in clause

Suppose I have the following query :
select value from table where value in ('abc','cde','efg');
If only 'abc' is populated in the table,
I want to be able to see which value is missing in the result set,
so the results looks like :
cde
efg
You can use UNION ALL to get a resultset with all the values that you want:
SELECT 'abc' AS value FROM dual UNION ALL
SELECT 'cde' FROM dual UNION ALL
SELECT 'efg' FROM dual
(you may omit FROM dual depending on your database).
And with NOT EXISTS get all the values from the above resultset that do not appear in the table:
SELECT u.*
FROM (
SELECT 'abc' AS value FROM dual UNION ALL
SELECT 'cde' FROM dual UNION ALL
SELECT 'efg' FROM dual
) u
WHERE NOT EXISTS (SELECT 1 FROM tablename t WHERE t.value = u.value)

Oracle SQL order by Varchar2 with '_'

I've got a table with a varchar2 column. Sorting by that column did not give me the expected result:
with test (col) as
(select '_83_' from dual union all
select '_81_' from dual union all
select '4___' from dual union all
select '____' from dual
)
select * from test
order by col desc;
returns:
Col
1. '_83_'
2. '_81_'
3. '4___'
4. '____'
I did expect:
Col
1. '4___'
2. '_83_'
3. '_81_'
4. '____'
Can you explain this and help me to get '4___' to the start of my order by statement?
Edit Using Littlefoots statement for preproduceabillity...
Edit I am using Oracle 12c
Edit NLS_Sort is set to German language. This was the issue.
In my local database, NLS_SORT is set to BINARY so It is not reproducible.
WITH TEMO AS
(
SELECT '_83_' AS X FROM DUAL UNION ALL
SELECT '_81_' AS X FROM DUAL UNION ALL
SELECT '4___' AS X FROM DUAL UNION ALL
SELECT '____' AS X FROM DUAL
)
SELECT * FROM TEMO ORDER BY X DESC;
X
----
____
_83_
_81_
4___
But, after changing NLS_SORT from BINARY to GERMAN, the issue is reproduced.
ALTER SESSION SET NLS_SORT=GERMAN;
WITH TEMO AS
(
SELECT '_83_' AS X FROM DUAL UNION ALL
SELECT '_81_' AS X FROM DUAL UNION ALL
SELECT '4___' AS X FROM DUAL UNION ALL
SELECT '____' AS X FROM DUAL
)
SELECT * FROM TEMO ORDER BY X DESC;
X
----
_83_
_81_
4___
____
You can check the NLS values using the following table:
NLS_SESSION_PARAMETERS
NLS_DATABASE_PARAMETERS
so the conclusion is NLS_SORT parameter must be set accordingly because not everyone wants to sort using technique.
Default value of NLS_SORT is derived from NLS_LANGUAGE.
Refer oracle documents for more information about NLS_SORT.
Solution is to change NLS_SORT according to requirement.
Cheers!!
I don't understand the question. Yes, a comment would be more appropriate but I can't post this:
SQL> with test (col) as
2 (select '_83_' from dual union all
3 select '_81_' from dual union all
4 select '4___' from dual union all
5 select '____' from dual
6 )
7 select * from test
8 order by col;
COL
----
____
4___
_81_
_83_
SQL>
As you can see, my results differ from yours, i.e. I can't reproduce what you are saying. Could you explain it once again, please?
Using Oracle 11g R2:
Select Column1 From (
SELECT CAST( '_83_' AS varchar2(4) ) AS Column1 FROM dual
union all
SELECT CAST( '_81_' AS varchar2(4) ) AS Column1 FROM dual
union all
SELECT CAST( '4___' AS varchar2(4) ) AS Column1 FROM dual
union all
SELECT CAST( '____' AS varchar2(4) ) AS Column1 FROM dual
) A order by Column1 desc
Output:
____
_83_
_81_
4___
Personally I'd recommend Tejash's answer but you can also fudge it with something like:
SELECT * FROM table ORDER BY TRANSLATE(col, '_', 'z') desc;

Oracle sql order by with character in order

Good day, Maybe someone can help me , i want to get n select output on oracle sql , but when i order by it is not correct.
code_order
1.
2.
2.1
3.
4.2
10.1
10.0
21.
21.1
23.
31.
it needs to be ordered numeric and all values has a full stop in.
any ideas??
Thanx, but i see that some values can contain non numeric values as well like C,B etc.
7C. 40. 50. 51. 6.
Table outline :
code_order is varchar2
use order by to_number(code_order)
with
val as
(
SELECT '1.' as c FROM dual
union all
SELECT '2.' as c FROM dual
union all
SELECT '3.' as c FROM dual
union all
SELECT '2.1' as c FROM dual
union all
SELECT '4.2' as c FROM dual
union all
SELECT '10.1' as c FROM dual
union all
SELECT '21.' as c FROM dual
union all
SELECT '10.0' as c FROM dual
union all
SELECT '21.1' as c FROM dual
union all
SELECT '23.' as c FROM dual
union all
SELECT '31.' as c FROM dual
)
SELECT c FROM val
order by to_number(regexp_replace(c, '^(\d+)\..*$', '\1'))
;

Oracle: transpose table

I've got the following query
SELECT 1, 2 FROM DUAL
AND I'd like something like
SELECT TRANSPOSE(SELECT 1, 2 FROM DUAL)
Which outputs the same as
SELECT 1 FROM DUAL
UNION
SELECT 2 FROM DUAL
I'd like it to swap lines with columns.
Assuming this is Oracle 11, you can use UNPIVOT:
select no from
(SELECT 1 a, 2 b FROM DUAL) dummy
unpivot (no for col in (a as 'A', b as 'B'))
Using dbms_xmlgen.getxmltype and XMLTABLE:
SELECT *
FROM XMLTABLE('/ROWSET/ROW/*' passing dbms_xmlgen.getxmltype('SELECT 1, 2, 3 FROM DUAL')
COLUMNS val VARCHAR(100) PATH '.');
db<>fiddle demo
Advantage over unpivot - there is no need to specify column list in advance
If you don't have Oracle 11g, the best solution is the one you provided:
SELECT 1 FROM DUAL
UNION
SELECT 2 FROM DUAL