Oracle, show only rows where no numeric values appear - sql

I have column X with values like:
619 19th St S, Oslo, AL 3522310, Spain
4538 S Harvard Ave, Roma, OK 74135, Germany
Golaa, CA , USA
Piri, SO, Italy
And I would like to filter only those, where I see no number in column so the outcome of the query should be:
Golaa, CA , USA
Piri, SO, Italy

I would use a regular expression, but I think this is simpler:
SELECT *
FROM yourTable
WHERE NOT REGEXP_LIKE(x, '[0-9]');
You can also do this without regular expressions:
WHERE x = TRANSLATE(x, 'a0123456789', 'a')

You can use Regular Expressions for pattern matching in Oracle.
SELECT
*
FROM
yourTable
WHERE
NOT REGEXP_LIKE(x, '[0-9]+')
This will exclude any rows that have one or more numeric digits in column x.

with s as (
select '619 19th St S, Oslo, AL 3522310, Spain' str from dual union all
select '4538 S Harvard Ave, Roma, OK 74135, Germany' from dual union all
select 'Golaa, CA , USA' from dual union all
select 'Piri, SO, Italy' from dual)
select *
from s
where str = translate(str, 'z1234567890', 'z');
STR
------------------------------
Golaa, CA , USA
Piri, SO, Italy

Related

Regular Expression for zipcode pattern?

How to write regular expression for zipcode pattern? I need to makesure that all zipcodes(sample addresses) are 5 digits, but my query is not working.
with table1 as(
select "123 6th St. Melbourne, FL 32904" as address union all
select "71 Pilgrim Avenue, Chevy Chase, MD 20815" union all
select "70 Bowman St. South Windsor, CT 06074" union all
select "4 Goldfield Rd. Honolulu, HI 966815" union all
select "44 Shirley Ave. West Chicago, IL 60185" union all
select "514 S. Magnolia St. Orlando, FL 32806 "
)
select address,regexp_contains("address",r"\s\d{5}$")check from table1
At least remove quotes around address in regexp_contains
select address, regexp_contains(address, r"\s\d{5}$") check from table1
Also you might want to revisit use of $ at the end of regex
Consider r"\b\d{5}\b" as an option

Insert space in between string Regecp Oracle

I have some addresses not in proper format. I want to add a space if there is none. Example shown below
Input Expected output
----- ----------------
AVEX AVE X or AVENUE X
AVE X AVE X or AVENUE X
AVENUEX AVENUE X or AVE X
AVENUE X AVENUE X or AVE X
AVEOFCITY AVE OF CITY or AVENUE OF CITY
I created a below expression ,but it is not giving correct result for all cases, especially the AVENUE breaks into AVE NUE
SELECT REGEXP_REPLACE('AVENUEN','^(AVE(NUE)*?)(\w)','\1 \3') rep FROM dual;
This will get you a little closer. Just tweaked your regex a little to allow for an optional 'NUE' and handle 0 or more spaces after.
with tbl(id, str) as (
select 1, 'AVEX' from dual union all
select 2, 'AVE X' from dual union all
select 3, 'AVENUEX' from dual union all
select 4, 'AVENUE X' from dual union all
select 5, 'AVEOFCITY' from dual
)
SELECT
id,
REGEXP_REPLACE(str,'^(AVE(NUE)?) *?(\w)','\1 \3') rep
FROM tbl;
You may need another pass to handle the 'OFCITY' as who knows what could come after the AVENUE that you have to allow for.

How to split a cell and create a new row in sql

I have a column which stores multiple comma separated values. I need to split it in a way so that it gets split into as many rows as values in that column along with remaining values in that row.
eg:
John 111 2Jan
Sam 222,333 3Jan
Jame 444,555,666 2Jan
Jen 777 4Jan
Output:
John 111 2Jan
Sam 222 3Jan
Sam 333 3Jan
Jame 444 2Jan
Jame 555 2Jan
Jame 666 2Jan
Jen 777 4Jan
P.S : I have seen multiple questions similar to this, but could not find a way to split in such a way.
This solution is built on Vertica, but it works for every database that offers a function corresponding to SPLIT_PART().
Part of it corresponds to the un-pivoting technique that works with every ANSI compliant database platform that I explain here (just the un-pivoting part of the script):
Pivot sql convert rows to columns
So I would do it like here below. I'm assuming that the minimalistic date representation is part of the second column of a two-column input table. So I'm first splitting that short date literal away, in a first Common Table Expression (and, in a comment, I list that CTE's output), before splitting the comma separated list into tokens.
Here goes:
WITH
-- input
input(name,the_string) AS (
SELECT 'John', '111 2Jan'
UNION ALL SELECT 'Sam' , '222,333 3Jan'
UNION ALL SELECT 'Jame', '444,555,666 2Jan'
UNION ALL SELECT 'Jen' , '777 4Jan'
)
,
-- put the strange date literal into a separate column
the_list_and_the_date(name,list,datestub) AS (
SELECT
name
, SPLIT_PART(the_string,' ',1)
, SPLIT_PART(the_string,' ',2)
FROM input
)
-- debug
-- SELECT * FROM the_list_and_the_date;
-- name|list |datestub
-- John|111 |2Jan
-- Sam |222,333 |3Jan
-- Jame|444,555,666|2Jan
-- Jen |777 |4Jan
,
-- ten integers (too many for this example) to use as pivoting value and as "index"
ten_ints(idx) AS (
SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
UNION ALL SELECT 10
)
-- the final query - pivoting prepared input using a CROSS JOIN with ten_ints
-- and filter out where the SPLIT_PART() expression evaluates to the empty string
SELECT
name
, SPLIT_PART(list,',',idx) AS token
, datestub
FROM the_list_and_the_date
CROSS JOIN ten_ints
WHERE SPLIT_PART(list,',',idx) <> ''
;
name|token|datestub
John|111 |2Jan
Jame|444 |2Jan
Jame|555 |2Jan
Jame|666 |2Jan
Sam |222 |3Jan
Sam |333 |3Jan
Jen |777 |4Jan
Happy playing ...
Marco the Sane

Oracle arrangements

So I have this issue, I have this table Locations{id,name}. In a query how can I get the number of different permutations two by two?
For example I have three locations:
Portugal
Spain
UK
The combinations are
Portugal,Spain
Spain Portugal
Portugal,UK
UK,Portugal
Spain,UK
UK,Spain
So the returned value should be 6. I can't get my head around this problem.
Do like this :
SELECT COUNT(1) FROM (SELECT t1.name as name1, t2.name as name2
FROM Locations t1, Locations t2
WHERE t1.name <> t2.name)
Here is an example http://sqlfiddle.com/#!4/41a10/3
As others have stated, if you just want the number, that's easy to do as it's just a math problem.
If you actually want all the combinations, you're looking for a cartesian product, except you want identical pairs filtered out.
See below -
with tbl as(
select '01' as id, 'Portugal' as name from dual union all
select '02' as id, 'Spain' as name from dual union all
select '03' as id, 'UK' as name from dual
)
select x.name, y.name
from tbl x, tbl y
where x.name <> y.name
Result:
NAME NAME
Portugal Spain
Portugal UK
Spain Portugal
Spain UK
UK Portugal
UK Spain
The easiest way is just to apply the right formula:
select count(*) * (count(*) - 1)
from locations l;
If you might have duplicates, then use count(distinct):
select count(distinct name) * (count(distinct name) - 1)
from locations l;
The number of two-way combinations for n things is n*(n - 1). You would divide the above by 2 if pairs were the same regardless of order.

Combine many tables in Hive using UNION ALL?

I'm trying to append one variable from several tables together (aka row-bind, concatenate) to make one longer table with a single column in Hive. I think this is possible using UNION ALL based on this question ( HiveQL UNION ALL ), but I'm not sure an efficient way to accomplish this?
The pseudocode would look something like this:
CREATE TABLE tmp_combined AS
SELECT b.var1 FROM tmp_table1 b
UNION ALL
SELECT c.var1 FROM tmp_table2 c
UNION ALL
SELECT d.var1 FROM tmp_table3 d
UNION ALL
SELECT e.var1 FROM tmp_table4 e
UNION ALL
SELECT f.var1 FROM tmp_table5 f
UNION ALL
SELECT g.var1 FROM tmp_table6 g
UNION ALL
SELECT h.var1 FROM tmp_table7 h;
Any help is appreciated!
Try with following coding...
Select * into tmp_combined from
(
SELECT b.var1 FROM tmp_table1 b
UNION ALL
SELECT c.var1 FROM tmp_table2 c
UNION ALL
SELECT d.var1 FROM tmp_table3 d
UNION ALL
SELECT e.var1 FROM tmp_table4 e
UNION ALL
SELECT f.var1 FROM tmp_table5 f
UNION ALL
SELECT g.var1 FROM tmp_table6 g
UNION ALL
SELECT h.var1 FROM tmp_table7 h
) CombinedTable
Use with the statement :
set hive.exec.parallel=true
This will execute different selects simultaneously otherwise it would be step by step.
I would say that's both straightforward and efficient way to do the row-bind, at least, that's what I would use in my code.
Btw, it might cause you some syntax error if you put your pseudo code directly, you may try:
create table join_table as
select * from
(select ...
join all
select
join all
select...) tmp;
I did same concept but for different tables employee and location that might help you I believe :
DATA:Table_e-employee
empid empname
13 Josan
8 Alex
3 Ram
17 Babu
25 John
Table_l-location
empid emplocation
13 San Jose
8 Los Angeles
3 Pune,IN
17 Chennai,IN
39 Banglore,IN
hive> SELECT e.empid AS a ,e.empname AS b FROM employee e
UNION ALL
SELECT l.empid AS a,l.emplocation AS b FROM location l;
OutPut with alias a and b:
13 San Jose
8 Los Angeles
3 Pune,IN
17 Chennai,IN
39 Banglore,IN
13 Josan
8 Alex
3 Ram
17 Babu
25 John