The records I am querying for are kept in 2 different formats. Each person has at least 1 record of their email in the format John.Doe#abc.com. Some people have a second record in which their email is DoeJ#abc.com.
How can I query for the records in which the email is formatted like John.Doe#abc.com?
I attempted to do it with the following SQL Statement but I it returns an empty result:
Select * from email where regexp_like(emailaddress, '. (#)')
The end product will be used in a join with a few other Queries, so selecting distinct values is not an option here. The environment is an Oracle DB, and because this will be done through multiple joins, the more efficient it is the better. Does anyone have any ideas what I am doing wrong, or other ways to accomplish this?
Thank you,
Joshua
You can use REGEXP_LIKE:
Select * from email where regexp_like(emailaddress, '\S*\.\S*\#\S*\.\S*')
Use "\S*" to match all non-whitespace characters
Or just a regular LIKE:
Select * from email where emailaddress LIKE '%.%#%.%'
Not sure what characters are included in the % placeholder in Oracle, so you should test it out.
The REGEXP one will give you tighter control over the pattern matching.
Let me know if it works.
How about using like?
where emailaddress like '%.%#%'
The first format seems distinguished by having a period before the ampersand.
Related
Got a apparently simple problem resolvable.
I want to update emails in my sql table with one query.
All emails ending by #notgoodexample.com (knowing I have several pattern that my email will match)
Should after that query be #goodexample.com
And I need to keep what is before the '#'.
My best try for the moment is
Update tableName SET table.email_addresse = concat(table.name, table.lastname, '#goodexample.com' --cheating a little bit cause these addresses are name.lastname#
WHERE email_address LIKE '%pattern%'
OR email_address LIKE '%pattern2%'
Do you have any tips for me ?
is it possible to compare a string against a list using "like" and wildcards, so sth like
select column
from table
where column like('%foo%', '%bar%')
The example does not work in any database format I know. How can I do this without using a verbose solution like
select column
from table
where column like'%foo%' or column like '%bar%'
I am interested in a platform-independent solution but mainly Sybase ASE.
Thanks very much!
If you are using MySQL/PostgreSQL you could use regexp, that is one way.
SELECT column FROM table WHERE (column REGEXP '^ALA[0-9]')
http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp
http://www.oreillynet.com/pub/a/databases/2006/02/02/postgresq_regexes.html
Second solution would be, mentioned by you creating many likes, joined by or.
i want to select name that only contain one word with SQL wildcards..
i have tried
select name from employee where name not like '% %'
it works,but i wonder if there are other ways to do it using SQL wildcards
note : i am a college student,i am studying wildcards right now . i was just wonder if there are other ways to show data that only contain one word with wildcards except the above..
Your method makes proper use of wildcards, alternatively you could do it with CHARINDEX or similar function depending on RDBMS
select name
from employee
where CHARINDEX(' ',name) = 0
Likewise the patindex function or similar use wildcards, but that's pretty much the same as CHARINDEX, just allows for patterns, so if looking for multiple spaces it would be helpful. I don't think there's much in the way of variation from your method for using wildcards.
If you have large database I would suggest to create new indexed column word_count which would be autofilled by insert/update trigger. Thus you will be able to search for such records more efficiently.
That's the way I'd do it using wildcards. The other way would be:
select name
from employee
where charindex(' ', name) = 0
I have this query:
select * from table where column like '%firstword[something]secondword[something]thirdword%'
What do I replace [something] with to match an unknown number of spaces?
Edited to add: % will not work as it matches any character, not just spaces.
Perhaps somewhat optimistically assuming "unknown number" includes zero.
select *
from table where
REPLACE(column_name,' ','') like '%firstwordsecondwordthirdword%'
The following may help: http://blogs.msdn.com/b/sqlclr/archive/2005/06/29/regex.aspx
as it describes using regular expressions in SQL queries in SQL Server 2005
I would definitely suggest cleaning the input data instead, but this example may work when you call it as a function from the SELECT statement. Note that this will potentially be very expensive.
http://www.bigresource.com/MS_SQL-Replacing-multiple-spaces-with-a-single-space-9llmmF81.html
I have the following SQL query:
SELECT Phrases.*
FROM Phrases
WHERE (((Phrases.phrase) Like "*ing aids*")
AND ((Phrases.phrase) Not Like "*getting*")
AND ((Phrases.phrase) Not Like "*contracting*"))
AND ((Phrases.phrase) Not Like "*preventing*"); //(etc.)
Now, if I were using RegEx, I might bunch all the Nots into one big (getting|contracting|preventing), but I'm not sure how to do this in SQL.
Is there a way to render this query more legibly/elegantly?
Just by removing redundant stuff and using a consistent naming convention your SQL looks way cooler:
SELECT *
FROM phrases
WHERE phrase LIKE '%ing aids%'
AND phrase NOT LIKE '%getting%'
AND phrase NOT LIKE '%contracting%'
AND phrase NOT LIKE '%preventing%'
You talk about regular expressions. Some DBMS do have it: MySQL, Oracle... However, the choice of either syntax should take into account the execution plan of the query: "how quick it is" rather than "how nice it looks".
With MySQL, you're able to use regular expression where-clause parameters:
SELECT something FROM table WHERE column REGEXP 'regexp'
So if that's what you're using, you could write a regular expression string that is possibly a bit more compact that your 4 like criteria. It may not be as easy to see what the query is doing for other people, however.
It looks like SQL Server offers a similar feature.
Sinec it sounds like you're building this as you go to mine your data, here's something that you could consider:
CREATE TABLE Includes (phrase VARCHAR(50) NOT NULL)
CREATE TABLE Excludes (phrase VARCHAR(50) NOT NULL)
INSERT INTO Includes VALUES ('%ing aids%')
INSERT INTO Excludes VALUES ('%getting%')
INSERT INTO Excludes VALUES ('%contracting%')
INSERT INTO Excludes VALUES ('%preventing%')
SELECT
*
FROM
Phrases P
WHERE
EXISTS (SELECT * FROM Includes I WHERE P.phrase LIKE I.phrase) AND
NOT EXISTS (SELECT * FROM Excludes E WHERE P.phrase LIKE E.phrase)
You are then always just running the same query and you can simply change what's in the Includes and Excludes tables to refine your searches.
Depending on what SQL server you are using, it may support REGEX itself. For example, google searches show that SQL Server, Oracle, and mysql all support regex.
You could push all your negative criteria into a short circuiting CASE expression (works Sql Server, not sure about MSAccess).
SELECT *
FROM phrases
WHERE phrase LIKE '%ing aids%'
AND CASE
WHEN phrase LIKE '%getting%' THEN 2
WHEN phrase LIKE '%contracting%' THEN 2
WHEN phrase LIKE '%preventing%' THEN 2
ELSE 1
END = 1
On the "more efficient" side, you need to find some criteria that allows you to avoid reading the entire Phrases column. Double sided wildcard criteria is bad. Right sided wildcard criteria is good.