Parse email address in Oracle to count number of addresses with 3 or less chars before # sign - sql

I need to count number of email addresses in a db that have 3 or less characters before the # sign, for example ab#test.com.
The parsename function isn't present in Oracle and I'm not sure how to write a regexp for this. Any help would be greatly appreciated!

Regex is overkill for this. All you need is
instr(t.email, '#') < 5 AND instr(t.email, '#') > 0
Edited with correction from the comments.

The regular expression you want is:
^[^#]{0,3}#
In English, that's:
Start of string
between 0 and three things that aren't an at sign.
an at sign.

You could define the WHERE clause & use COUNT, or skip to use REGEXP_COUNT instead:
SELECT REGEXP_COUNT(t.email, '^[^#]{0,3}#', 1, 'i') RESULT
FROM TABLE t;
Using COUNT:
SELECT COUNT(*)
FROM TABLE t
WHERE REGEXP_LIKE(t.email, '^[^#]{0,3}#')
Reference:
Oracle Regular Expression Syntax
http://www.regular-expressions.info/

There is a little problem with following
instr(t.email, '#') < 5
this query will work provided t.email has a '#' ! other wise it will return those entries also where t.email is not having '#'

Related

SQL name workers with only 5 characters

I have a database with workers and their names. How can I get a list of the workers whose name contains only 5 characters
What about this?
SELECT * FROM table_name WHERE island_name LIKE '_____'
Alternatively,
SELECT * FROM table_name WHERE REPLICATE('A',LEN(island_name)) = 'AAAAA'
You could use SUBSTRING(), if you are looking for an alternative method. You can check if a SUBSTRING of X characters == The Original String.
However, you would also need to account for 4-character strings, for example. You could add "padding characters", or you could make sure that X-1 Character Substring != X-character Substring. In Sql 2016 for example, these are the same with at least one case of query options:
SELECT SUBSTRING('ISLA',1,4)
SELECT SUBSTRING('ISLA',1,5)
I agree that LENGTH is the best option. Maybe this is a school question.
Give this a try:
SELECT SUBSTR(field1,1,5)
FROM table1
WHERE substr(field1,5,1) IS NOT NULL
AND SUBSTR(segment1,6,999) IS NULL;;

Regular expression for gettin data after - in sql

I have a column with assignment numbers like - 11827,27266,91717,09818-2,726252-3,8716151-0,827272,18181
Now i am selecting the records like
select assignment_number from table;
But now i want that the column detail is retreived in such a way that numbers are only retrieved without -2 -3 etc like
726252-3---> 726252 8716151-0-->8716151
I know i can use regex for this but i do not know how to use it
This will select everthing before the character -:
^([^-]+)
From 726252-3 will match 726252
You would use regexp() substr:
select regexp_substr(assignmentnumber, '[0-9]+')
This will return the first string of numbers encountered in the string.

SQL : REGEX MATCH - Character followed by numbers inside quotes

I have a column in sql which holds value inside double quotes like "P1234567" , "P1234" etc..
I need to identify only columns which start with letter P and is followed by seven digits (numbers) only. I tried where column like'"P[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"' but it doesn't seem to work.
Can someone please correct me or point me to a thread which can help me out?
Thanks
Standard SQL has no regex support, but most SQL engines have regex extensions added to them on top of the standard SQL. So, for example, if you're using MySQL then you'd do this:
... WHERE column REGEXP '^"P[0-9]{7}"'
And if you're using Postgres then that would be:
... WHERE column ~ '^"P[0-9]{7}"'
(updated to match the double-quote part of the question, I'd misunderstood that to begin with)
How about using length and isnumeric:
Select
*
from
mytable
where
mycolumn like '"P%'
and len(mycolumn) = 10 --2 chars for quotes + 1 for 'P' + 7 for the digits
and isnumeric(substring(mycolumn, 3, 7))=1
This answer is for SQL Server, other DBMS's may have a different syntax for length

Regular expression to return number after matched string in oracle

I have a query:
select ITEM_ID from system_items where id=4020;
I want a regular expression that takes the above query as input and matches for pattern "id=" and returns 4020.
Please let me know if you have any suggestions, as I have been trying with REGEXP_SUBSTR in Oracle and couldn't get it.
REGEX_SUBSTR won't allow a look-behind like (?<=id=\s*)\d+ so I suspect you need to do this in two operations. First get id=4020, then strip the id=.
One possible way of doing that would be:
REGEXP_SUBSTR(REGEXP_SUBSTR(a, 'id=\s*\d+'), '\d+')
SQLFiddle
This should do it
/id=(\d+)/
id is literal match
() are used for making the capture groups
\d is more numbers
+ ensures 1 or more
demo here http://rubular.com/r/GBxfhID5hS

Matching exactly 2 characters in string - SQL

How can i query a column with Names of people to get only the names those contain exactly 2 “a” ?
I am familiar with % symbol that's used with LIKE but that finds all names even with 1 a , when i write %a , but i need to find only those have exactly 2 characters.
Please explain - Thanks in advance
Table Name: "People"
Column Names: "Names, Age, Gender"
Assuming you're asking for two a characters search for a string with two a's but not with three.
select *
from people
where names like '%a%a%'
and name not like '%a%a%a%'
Use '_a'. '_' is a single character wildcard where '%' matches 0 or more characters.
If you need more advanced matches, use regular expressions, using REGEXP_LIKE. See Using Regular Expressions With Oracle Database.
And of course you can use other tricks as well. For instance, you can compare the length of the string with the length of the same string but with 'a's removed from it. If the difference is 2 then the string contained two 'a's. But as you can see things get ugly real soon, since length returns 'null' when a string is empty, so you have to make an exception for that, if you want to check for names that are exactly 'aa'.
select * from People
where
length(Names) - 2 = nvl(length(replace(Names, 'a', '')), 0)
Another solution is to replace everything that is not an a with nothing and check if the resulting String is exactly two characters long:
select names
from people
where length(regexp_replace(names, '[^a]', '')) = 2;
This can also be extended to deal with uppercase As:
select names
from people
where length(regexp_replace(names, '[^aA]', '')) = 2;
SQLFiddle example: http://sqlfiddle.com/#!4/09bc6
select * from People where names like '__'; also ll work