SQL RLIKE function Postcode Search - regex

I am trying to understand why the following query pulls through postcodes that I wouldn't expect.
SQL
Select distinct Postcode from tableA where like 'NE1%';
Shows 2 postcodes, all beginning with NE1
I've tried :-
Select distinct Postcode from tableA where rlike '^NE[0-1]%'
Shows many postcodes, including the 2 from above, such as NE27 0EZ - I'm assuming because it has a zero in the 2nd part of the postcode, but no idea why NE2 2NE appears !
My goal is to filter all postcodes that begin with an N (not NE) BUT only have a numeric as the next character - SQL only, not python or scala, as this filter forms 1 of many postcode filters (a large OR clause)
I would have thought for all postcodes beginning with a N that had a numeric as the next character would have worked :-
Select distinct Postcode from tableA where rlike 'N[0-9] %' or 'N[0-9][0-9] %'
select distinct 'rlike' as Func , postcode from npex.npex where postcode rlike '^NE[0-1]*'
union
select distinct 'like', postcode from npex.npex where postcode like 'NE1%'
order by 1;
RESULTS
Func postcode
like NE1 3BB
like NE12 1AB
rlike NE27 0EZ
rlike NE6 2UT
rlike NE27 0LT
rlike NE12 1AB
rlike NE2 2NE
rlike NE3 4DT
rlike NE1 3BB

* is not needed, otherwise you would be matching 0 or more of zeroes or ones.
select distinct postcode from npex.npex where postcode rlike '^NE[0-1]'
If you want to get those beginning with an N followed by a numeric, you can use
select distinct postcode from npex.npex where postcode rlike '^N[0-9]'

Related

Hive query to extract a column which has alphanumeric characters

I have a requirement in which I need to extract the data based on a filter on a column and the filter would be to extract only alphanumeric values which means that it should contain at least one alphabet and a number for consideration.
For example if I have five numbers such as 333,abc,ab333,+33,+ab33 the output should have only ab333 and +ab33.
I was trying to implement this using the rlike function and the query is as below but this is giving all the records in the table.
select column_name from table_name where column_name rlike '^[a-zA-Z0-9]+$';
I also tried a different approach by using the below query but in case of special characters such as + the below query gives the wrong result.
select column_name from table_name where column_name not rlike '^[0-9]+$';
Could anybody guide me regarding the mistake of if there is a different approach for this.
You can use
RLIKE '^\\+?(?:[0-9]+[a-zA-Z]|[a-zA-Z]+[0-9])[0-9a-zA-Z]*$'
Details:
^ - start of string
\+? - an optional + symbol
(?:[0-9]+[a-zA-Z]|[a-zA-Z]+[0-9]) - one or more digits followed with a letter or one or more letters followed with a digit and then
[0-9a-zA-Z]* - zero or more alphanumeric chars
$ - end of string.

Create a new column by executing regular expression on existing column

I have column with data as follows:
p=Chicago, IL|q=rental houses
My goal is to obtain
Chicago IL rental houses as the outcome by running regular expression on the column via a select query.
Use below regx on string
/p=(.*)|q=(.*)/
Then join 2 substrings with spaces.
If you want get result from select query you can use select with concat or concat_ws function instead.

Extract rows from a table with regular expression hive sql

Please check the link for the result and table info. I need to query rows
with value '343' in Col B with a regular expression . All columns are strings . Also please be kind enough to point any good learning materials in how to write good REGEX in Hive . Thank you
For Hive use this:
select * from tablename where B rlike '343';
Checking it works:
hive> select '123435' rlike '343';
OK
_c0
true
Negative test:
hive> select '12345' rlike '343';
OK
_c0
false
Time taken: 1.675 seconds, Fetched: 1 row(s)
Hive uses Java flavor regex. You can find good reference and practice here: https://regexr.com/ and of course regex101
this will work:
select * from tablename where regexp_like(B,'(.*)(343)(.*)');
hive equivalent is :
select * from tablename where rlike(B,'(.*)(343)(.*)');

HiveQL - Rlike to a regexp in a field?

Can you use rlike to join a table using regular expressions contained in a field?
i.e.
Select a., b.
from Table a
inner join Table2 b
on a.Field rlike b.Field2
i.e. Table 2 data:
Field1 Field2
David ^D(a|o)vid
Test a ^Test
Just worked out that I can do this.
select a., b.
from Table1 a, Table2 b
where b.Field1 rlike a.Field2

Validate column using regular expression in postgre SQL

I need to check whether a column in a table having a numeric value followed by decimal point and 3 precisions after the decimal point. Kindly suggest how to do using regular expression in postgre SQL (or) any other alternate method.
Thanks
The basic regex for digits, a period and digits is \d+\.\d{3}
You can use it for several things, for instance:
1. Add a Constraing to your Column Definition
ALTER TABLE mytable ADD (CONSTRAINT mycolumn_regexp CHECK (mycolumn ~ $$^\d+\.\d{3}\Z$$));
2. Find Rows that Don't Match
SELECT * FROM mytable WHERE mycolumn !~ $$^\d+\.\d{3}\Z$$;
3. Find Rows that Match
SELECT * FROM mytable WHERE mycolumn ~ $$^\d+\.\d{3}\Z$$;