When doing a SQL Query and I want to pull up entries from a table that have AND in the name, but I do not want names that just have and in them.....
Confusing, but I'll show an example
There are 2 Entries in the table:
Pandomniam
Frank and Beans.
When I do the query I just want Frank and Beans to come up, not Pandomniam. I am doing something like
SELECT NAME FROM TABLE WHERE NAME LIKE '%and%'
this will get me results, but I have uneeded ones.
My first instinct is to try and see if I can get just Pandomniam in a result so I could do an AND NOT LIKE to filter them out but I can't seem to come up with one.
SELECT NAME FROM TABLE
WHERE
NAME LIKE '% and %' OR
NAME LIKE 'and %' OR
NAME LIKE '% and'
SELECT NAME FROM TABLE WHERE NAME LIKE '% and %'
How about adding a couple spaces:
SELECT NAME FROM TABLE WHERE NAME LIKE '% and %'
You don't say what brand of RDBMS you're using. MySQL supports a regular-expression predicate RLIKE with a metacharacter sequence for "word boundary":
SELECT NAME FROM TABLE WHERE NAME RLIKE '[[:<:]]and[[:>:]]'
If you're using another brand of database other than MySQL, perhaps you can research its regular expression features.
Related
I need to select rows that contains a whole word, NOT a substring. For example from these rows:
System works good
I have some goodies
I need to get only first one (that contains word good), but with my sql query I get both of them.
select *
from my_tab
where my_column like '%good%'
Please note, I need this in sqlite, where not all usual sql functions are available.
You can check it like a space between both % symbols and a and % int end of the word and a % and to the beginning of the word which you need to search.
Query
select * from your_table_name
where words like '% good %'
or words like 'good %'
or words like '% good';
Try
select *
from table
where ' '||words||' ' like '% good %'
Hello i have a table with colums called NAME, in this colum i can have this type of name : First name Uppercase and other lower (Jack), all name uppercase (JACK), and name with space (Jack ) or (JACK ).
How can show all name than have jack in all type ?
i need to search multiple name with IN statement
i use ORACLE
Assuming SQL Server:
SELECT Name FROM Table WHERE Name LIKE '%jack%'
SELECT Name
FROM Table
WHERE UPPER(Name) LIKE '%JACK%'
will work in all SQL. In SQL Server I believe LIKE is case insensitive but it might depend on configuration settings and collation.
or maybe you need
SELECT Name
FROM Table
WHERE UPPER(TRIM(Name)) IN ('JACK','HOGAN','VICTOR')
(replace TRIM(Name) with RTRIM(LTRIM(Name)) if not using SQL Server.)
If you have a table with the list of names you can do it like this:
SELECT Name
FROM Table
JOIN NameTable ON UPPER(RTRIM(LTRIM(Table.Name))) = NameTable.Name
I know you can use LIKE for a select statement to search for a string and then use wildcards to match that string.. ex:
Select * from table where first_name LIKE '%r%';
Would return names such as robert, roland, craig, etc.
I'm curious how you would search for an actual wildcard in the string such as the %:
Select * from table where values LIKE '% % %';
(the middle % being what you would be looking for) (obviously this is not the correct way to do it which is why I'm asking).
How about
LIKE '%\%%' ESCAPE '\'
http://docs.oracle.com/cd/B12037_01/server.101/b10759/conditions016.htm
Just LIKE '%%' will be even easier.
I have a sqlite3 query like:
SELECT word FROM table WHERE word NOT LIKE '%a%';
This would select all of the words where 'a' does not occur in the word. This I can get to work perfectly. The problem is if I want to further restrict the results to not include 'b' anywhere in the word. I am picturing something like this.
SELECT word FROM table WHERE word NOT IN ('%a%', '%b%', '%z%');
which this obviously does not work, but this is the idea. Just adding an AND clause is what I'm trying to avoid:
SELECT word FROM table WHERE word NOT LIKE '%a%' AND NOT LIKE '%b%';
If this is the only option then I will have to work with that, but I was hoping for something else.
SELECT word FROM table WHERE word NOT LIKE '%a%'
AND word NOT LIKE '%b%'
AND word NOT LIKE '%c%';
If you use Sqlite's REGEXP support ( see the answer at Problem with regexp python and sqlite for how to do that ) , then you can do it easily in one clause:
SELECT word FROM table WHERE word NOT REGEXP '[abc]';
You missed the second statement: 1) NOT LIKE A, AND 2) NOT LIKE B
SELECT word FROM table WHERE word NOT LIKE '%a%' AND word NOT LIKE '%b%'
this is a select command
FROM
user
WHERE
application_key = 'dsfdsfdjsfdsf'
AND email NOT LIKE '%applozic.com'
AND email NOT LIKE '%gmail.com'
AND email NOT LIKE '%kommunicate.io';
this update command
UPDATE user
SET email = null
WHERE application_key='dsfdsfdjsfdsf' and email not like '%applozic.com'
and email not like '%gmail.com' and email not like '%kommunicate.io';
I'm not sure why you're avoiding the AND clause. It is the simplest solution.
Otherwise, you would need to do an INTERSECT of multiple queries:
SELECT word FROM table WHERE word NOT LIKE '%a%'
INTERSECT
SELECT word FROM table WHERE word NOT LIKE '%b%'
INTERSECT
SELECT word FROM table WHERE word NOT LIKE '%c%';
Alternatively, you can use a regular expression if your version of SQL supports it.
The query you are after will be
SELECT word FROM table WHERE word NOT LIKE '%a%' AND word NOT LIKE '%b%'
If you have any problems with the "not like" query,
Consider that you may have a null in the database. In this case, Use:
IFNULL(word, '') NOT LIKE '%something%'
Try out below query which worked for me.
SELECT word FROM table WHERE NOT (word LIKE '%a%' AND word LIKE '%b%');
Answer: Regular Expression
Regular Expression aka Regex (regexp) are so powerful and used by almost every popular programming language.
Using this you can find multiple patterns within few keystrokes instead of multiple AND, OR statement.
Simple RegExp for the above solution:
SELECT word FROM table WHERE word REGEXP '^[a-zA-Z]*[^abz][a-zA-Z]*$';
Here:
^ represent the starting of the text
[a-zA-Z] represent that any one character (upper/lower case) can take place
* represent that whatever is preceded can be repeated 0 or more times
[^abz] represent that, except these three characters any other character can take place - only single character
$ represent the ending of the text
Find a great example list here
I've got a simple nvarchar(25) column in an SQL database table. Most of the time, this field should contain alphanumeric text. However, due to operator error, there are many instances where it contains only a number. Can I do a simple search in SQL to identify these cases? That is, determine which rows in the table contain only digits in this column. As an extension, could I also search for those column values which contain only digits and a space and/or slash.
In other languages (eg. Perl, Java) a regular expression would resolve this quickly and easily. But I haven't been able to find the equivalent in SQL.
yes you can achive this by using isnumeric function available in sql sever
check more at this link : http://msdn.microsoft.com/en-us/library/aa933213(SQL.80).aspx
All the answers referred to the isnumeric function, but they are not correct, I've mentioned in a comment for all the answers the flaw in the answers. The correct solution is to use a regular expression in your where clause, which contains not like '%[^0-9]%', see the example below:
select column_name from table_name where column_name not like '%[^0-9]%'
select column_name from table_name where IsNumeric(column_name) <> 1
Numeric Only:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1
With Space:
SELECT * FROM Table WHERE Field LIKE '% %'
With Slash:
SELECT * FROM Table WHERE Field LIKE '%/%'
Combined:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1 OR Field LIKE '% %' OR Field LIKE '%/%'