how to selects rows based on a single value against a comma separated column in oracle? - oracle

I have a table structure like below:
TestTable:
ID(number)|Names(clob)
1 'a','b','c','d'
2 'b','c','d'
3 'g','h','e'
Now I want to select rows in which Names column contains 'b',so the expected output will be the first 2 columns.
How can do it in Oracle.
Please remember, the solution has to work in Oracle 10g as well as 11g.
Thanks in advance.

Try this using LIKE operator:
select
*
from your_table
where ',' || Names || ',' like '%,b,%';
Concatenated commas are to cover for the cases where b is present at the start or end of the string.

Related

how to find if a string exists in oracle column

i have a table that store mutiple values in column i need to find if the value exists then return entire row
Example : 963963963;35793579;741741
Query am using
select * from table where column like (%963%);
which return all the rows that have the value starts or ends with 963
i need only one row that match with 963
It is generally bad table design to store semicolon separated data this way. If you must continue with your current design, you may use the following query to find an exact match for 963:
SELECT *
FROM yourTable
WHERE ';' || col || ';' LIKE '%;963;%';

delete the character or string before nth delimiter '|'

I have a LOG_TAB table where column COLLEC_VAL values are Abcd#123|Mnbv#1234|Poiu#1234|Asdf#1234|....... in oracle. These values are not fixed. I want to keep only latest 4 string or character in the column and rest of the values will be deleted, that is Abcd#1234 will be deleted checking with delimiter(|).
After deleting first one, below values will be available:
Mnbv#1234|Poiu#1234|Asdf#1234|.......
how it will check that already 4 or grater than 3 delimiter exist and delete the first one? Please help.
Try this
update log_tab set collec_val = substr(collec_val, instr(collec_val, '|', -1, 4)+1)
this will help for all your columns try this on a table,it show you sample emp table's data:
select SUBSTR(listagg(ename,'|') within group (order by ename),instr(listagg(ename,'|') within group (order by ename),'|',-1,4)+1) employee_name from emp;

Distinct Column in Hive

I am trying to get a query result in HiveQL with one column as distinct. However the results are not matching . There are almost 20 columns in the table.
create table uniq_us row format delimited fields terminated by ',' lines terminated by '\n' as select distinct(a),b,c,d,e,f,g,h,i,j from ctry_us_join;
The resulting number of Rows :513238
select count(distinct a) from ctry_us_join;
The resulting number of rows : 151616
How is this possible and is something wrong in my first or second query
U need to use subselect with group by statement.
select count(a) from (
select a, count(*) from ctry_us_join group by a) b
This is just one solution for this.
Distinct is a keyword, not a function. It applies to all columns you list in your select clause. It is quite reasonable that your table has only 151,616 distinct values in the column a, but multiple rows with the same value in the column a have different values in other columns. That might give you 513,238 distinct rows.

Oracle parsing string for separators

I have one table in Oracle 11g database
I am storing some value like '|' separated mode as below
Table: ABC
FIELD: XYZ
Values :
9
1|12
52
5|112
Now I want to find ID from those values
I used REGEXP_LIKE but it will get all the rows containing value
Suppose I will search for 5 then it will give result for '5' and also for '52'
please help me to search particulate id from this field
Thanks
Try this.
select * from table_name
where '|' || column_name || '|' like '%|' || 'search_string' || '|%';
Sample here.

How to show star at first two character of a string in oracle query?

Example if an ID is 1213 i want show **13.
If it's a number
select '**' || substr(to_char(id),3)
from my_table
Or, if it's already a character
select '**' || substr(id,3)
from my_table
This concatenates ** onto the beginning of the string, using the Oracle concatenation operator || and removes the first two characters of the id using substr.
Here's a SQL Fiddle to demonstrate.
If you don't want to sacrifice performance too much, to mask first two characters you can use-
SQL> select regexp_replace('1213','(.)2','**') from dual; --if VARCHAR
MASKED
------------
**13
SQL> select regexp_replace(1213,'(.)2','**') from dual; --if NUMBER
MASKED
------------
**13
REGEXP_REPLACE will work alike on NUMBER and VARCHAR so you save some conversion time there.
Consecutively, you can create a Function Based Index on the regexp function operation to optimize the query like (considering you would always want to mask only first two characters of ID) -
CREATE INDEX
mask_id
ON
table_name
(regexp_replace(id,'(.)2','**'));

Resources