Display all the names of the employees whose names having letter A as 2nd occurrence without using like operator - sql

For the above query, I am using regexp_count but in SQL command line I am getting regexp_count :invaild identifier;
select * from table_name WHERE (REGEXP_COUNT(column_name, 'A')) >2;
Is this query works?

I'm not sure what you really want; is it to return names whose 2nd letter is "a", or names that have two or more letters "a" within.
Anyway, pick the one you find appropriate.
SQL> create table test as
2 (select 'saritha' col from dual union all
3 select 'mamatha' from dual union all
4 select 'vaisnavi' from dual union all
5 select 'sai' from dual union all
6 select 'vijaya' from dual union all
7 select 'kumar' from dual
8 );
Table created.
2 or more letters "a":
SQL> select col
2 from test
3 where regexp_count(col, 'a') >= 2;
COL
--------
saritha
mamatha
vaisnavi
vijaya
2nd letter is "a":
SQL> select col
2 from test
3 where substr(col, 2, 1) = 'a';
COL
--------
saritha
mamatha
vaisnavi
sai
SQL>
On Oracle 10g, which doesn't support REGEXP_COUNT function, one option is to replace all letters a with an empty string (basically, you'd remove all letters a) and fetch rows whose difference of full column length and "replaced" column length is >= 2. Something like this:
SQL> select col
2 from test
3 where length(col) - length(replace(col, 'a', '')) >= 2;
COL
--------
saritha
mamatha
vaisnavi
vijaya
SQL>

Related

Columns to row converting

Table_A:
AA
BB
CC
DD
Output:
AA,BB,CC,DD
Any one help this
LISTAGG seems to be one choice:
Sample data:
SQL> with table_a(col) as
2 (select 'AA' from dual union all
3 select 'BB' from dual union all
4 select 'CC' from dual union all
5 select 'DD' from dual
6 )
Query:
7 select listagg(col, ',') within group (order by col) result
8 from table_a;
RESULT
------------------------------
AA,BB,CC,DD
SQL>
informatica only solution -
SRT : sort the data by col1.
EXP : create 3 ports. make sure their length be >3000 char
in_col1 = col1
v_col1 = col1|v_col1
p_col1= col1
o_col1= v_col1
AGG: create 2 ports
in_col1 <-- link o_col1 from previous step2
o_col1 = MAX(in_col1) -- link this to final target
Normally, there should be a key column by which you want to aggregate the strings. if you have any, you need to change step2 in v_col1.

Find strings starting with alphanumeric in Oracle

I want to search for all records starting alphabet or number only.
I know there is REGEXP_LIKE to find if col contains alphanumeric but couldn't apply it for starting with.
SELECT * FROM mytable WHERE col1 like 'ABC:XYZ%'
I have data in below format:--
ABC:XYZ
ABC:XYZ (ERW)
ABC:XYZ TMN
ABC:XYZ123
ABC:XYZRTY:YER
I am trying to get only below output
ABC:XYZ
ABC:XYZ123
ABC:XYZRTY:YER
Regards
Something like this? Sample data up to line #7; query you might be interested in begins at line #8.
SQL> with mytable (col1) as
2 (select 'ABC:XYZ' from dual union all
3 select 'ABC:XYZ (ERW)' from dual union all
4 select 'ABC:XYZ TMN' from dual union all
5 select 'ABC:XYZ123' from dual union all
6 select 'ABC:XZYRTY:YER' from dual
7 )
8 select col1
9 from mytable
10 where not regexp_like(col1, '[^[:alnum:]:]');
COL1
--------------
ABC:XYZ
ABC:XYZ123
ABC:XZYRTY:YER
SQL>

How can I get a natural numeric sort order in Oracle?

I have a column with a letter followed by either numbers or letters:
ID_Col
------
S001
S1001
S090
SV911
SV800
Sfoofo
Szap
Sbart
How can I order it naturally with the numbers first (ASC) then the letters alphabetically? If it starts with S and the remaining characters are numbers, sort by the numbers. Else, sort by the letter. So SV911should be sorted at the end with the letters since it also contains a V. E.g.
ID_Col
------
S001
S090
S1001
Sbart
Sfoofo
SV800
SV911
Szap
I see this solution uses regex combined with the TO_NUMBER function, but since I also have entries with no numbers this doesn't seem to work for me. I tried the expression:
ORDER BY
TO_NUMBER(REGEXP_SUBSTR(ID_Col, '^S\d+$')),
ID_Col
/* gives ORA-01722: invalid number */
Would this help?
SQL> with test (col) as
2 (select 'S001' from dual union all
3 select 'S1001' from dual union all
4 select 'S090' from dual union all
5 select 'SV911' from dual union all
6 select 'SV800' from dual union all
7 select 'Sfoofo' from dual union all
8 select 'Szap' from dual union all
9 select 'Sbart' from dual
10 )
11 select col
12 from test
13 order by substr(col, 1, 1),
14 case when regexp_like(col, '^[[:alpha:]]\d') then to_number(regexp_substr(col, '\d+$')) end,
15 substr(col, 2);
COL
------
S001
S090
S1001
Sbart
Sfoofo
SV800
SV911
Szap
8 rows selected.
SQL>

Sort a value list that contains letters and also numbers in a specific order

I have a problem in SQL Oracle, I'm trying to create a view that contains values with letters and numbers and I want to sort them in a specific order.
Here is my query:
create or replace view table1_val (val, msg_text) as
select
val, msg_text
from
table_val
where
val in ('L1','L2','L3','L4','L5','L6','L7','L8','L9','L10','L11','L12','L13','L14','G1','G2','G3','G4')
order by lpad(val, 3);
The values are displayed like this:
G1,G2,G3,G4,L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13
The thing is that I want to display the L values first and then the G values like in the where condition. The 'val' column is VARCHAR2(3 CHAR). The msg_text column is irrelevant. Can someone help me with that? I use Oracle 12C.
You must interpret the second part of the val column as a number
order by
case when val like 'L%' then 0 else 1 end,
to_number(substr(val,2))
This work fine for your current data, but may fail in future if a new record is added with non-numeric structure.
More conservative (and more hard to write), but safe would be to used a decode for all the current keys, ordering unknown keys on the last position (id = 18 in the example):
order by
decode(
'L1',1,
'L2',2,
'L3',3,
'L4',4,
'L5',5,
'L6',6,
'L7',7,
'L8',8,
'L9',9,
'L10',10,
'L11',11,
'L12',12,
'L13',13,
'G1',14,
'G2',15,
'G3',16,
'G4',17,18)
You can't do anything based on the order of the WHERE condition
But you can use a CASE on the ORDER BY
ORDER BY CASE
WHEN SUBSTR(val, 1, 1) = 'L' THEN 1
WHEN SUBSTR(val, 1, 1) = 'G' THEN 2
ELSE 3
END,
TO_NUMBER (SUBSTR(val, 2, 10));
Another option to consider might be using regular expressions, such as
SQL> with table1_val (val) as
2 (select 'L1' from dual union all
3 select 'L26' from dual union all
4 select 'L3' from dual union all
5 select 'L21' from dual union all
6 select 'L11' from dual union all
7 select 'L4' from dual union all
8 select 'G88' from dual union all
9 select 'G10' from dual union all
10 select 'G2' from dual
11 )
12 select val
13 from table1_val
14 order by regexp_substr(val, '^[[:alpha:]]+') desc,
15 to_number(regexp_substr(val, '\d+$'));
VAL
---
L1
L3
L4
L11
L21
L26
G2
G10
G88
9 rows selected.
SQL>

Replace a column value in a string delimiter using Oracle SQL

I have a string value in a column in a table like
001|3880000005376|Personal ID| ||15-MAY-2006
and I want to replace the fourth value by another string value 'ABCDEF' , can it be possible by a single update or by PL/SQL program?
Here's one option:
SQL> with test (id, col) as
2 (select 1, '001|3880000005376|Personal ID| ||15-MAY-2006' from dual union all
3 select 2, '002|3880000005376|Personal ID|XXX||15-MAY-2007' from dual
4 )
5 select
6 id,
7 regexp_replace(col, '[^|]+', 'NEW STRING', 1, 4) result
8 from test;
ID RESULT
---------- ------------------------------------------------------------
1 001|3880000005376|Personal ID|NEW STRING||15-MAY-2006
2 002|3880000005376|Personal ID|NEW STRING||15-MAY-2007
SQL>
It replaces 4th occurrence of the '[^|]+' pattern with a NEW STRING value.