check if column contains one of many values - sql

i have an array of strings and a column which may contain one or more of those strings(seperated by space) i want to get all rows where this column contains one of the strings. Since the values all have 3 letters and therefore can't contain each other, i know i could just write
SELECT * FROM table WHERE
column LIKE '%val1%' OR
column LIKE '%val2%' OR
column LIKE '%val3%' OR
column LIKE '%val4%'
But i'm wondering if there isn't an easier statement, like column IN ('val1', 'val2', 'val3', 'val4') (This one seems only to work when the entry is equal to one of the values, but not if it just contains them)

Try reading this Is there a combination of "LIKE" and "IN" in SQL? and Combining "LIKE" and "IN" for SQL Server , this will solve you question.
Something like this from the first link.
SQL Server:
WHERE CONTAINS(t.something, '"bla*" OR "foo*" OR "batz*"')

Ist oracle you could use regular expressions
select *
from table
where regexp_like (column,'val(1|2|3|4)')

Related

PostgreSQL: return row where any column value like variable

I am trying to have the user search for a value in a SQL table, and the user is returned with any row that contains that value. At the moment, I can make it work such that the code is:
SELECT * FROM table WHERE lower('foo') in (lower('col1'),lower('col2'),etc)
However, I would like it to be able to search every column and return any row LIKE 'foo'. For instance,
SELECT * FROM table WHERE (lower('col1'), lower('col2'), etc) like lower('%foo%')
But that doesn't work.
Any suggestions?
I believe you need to use multiple WHERE clauses instead of grouping them all into one statement. Try this:
SELECT * FROM table
WHERE lower(col1) like lower('%foo%')
OR lower(col2) like lower('%foo%')
OR etc like lower('%foo%')
You can convert the whole row to a string and then use LIKE on the result of that:
select *
from the_table
where lower(the_table::text) like '%foo%';
the_table::text returns all columns of each row as a comma separated list enclosed with parentheses, e.g. (42,Arthur,Dent). So the above is not 100 identical to a LIKE condition applied on each column - but probably does what you want.

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

Delete rows if letters in cell SQL Server

I needed some guidance as to how I can exlude rows that contains any form of letter in a cell that is supposed to contain integers only.
This is what the data looks like now:
CustomerID
----------
ea176680
27906857
62675436
62566247
1bed413e
61110053
61113323
34441416
AS you can see in the table, the first,ID contains letters. How can I remove complete rows if a cell were to contain any form of letters?
How about this where clause?
where CustomerId not like '%[^0-9]%'
It filters out any values that have a non-digit. Unfortunately, SQL Server doesn't support regular expressions, but this will do what you want.
If you specifically wanted to look for letters, you can do:
where CustomerId like '%[a-zA-Z]%'
SQL Server can match on a specific pattern in your statement using like. It looks somewhat like regex, but isn't exactly that.
Use this statement if you want to match on all CustomerIds that contains characters in the range A-Z and a-z:
select *
from table
where CustomerID like '%[A-za-z]%'

SQLite WHERE-Clause for every column?

Does SQLite offer a way to search every column of a table for a searchkey?
SELECT * FROM table WHERE id LIKE ...
Selects all rows where ... was found in the column id. But instead to only search in the column id, I want to search in every column if the searchstring was found. I believe this does not work:
SELECT * FROM table WHERE * LIKE ...
Is that possible? Or what would be the next easy way?
I use Python 3 to query the SQLite database. Should I go the route to search through the dictionary after the query was executed and data returned?
A simple trick you can do is:
SELECT *
FROM table
WHERE ((col1+col2+col3+col4) LIKE '%something%')
This will select the record if any of these 4 columns contain the word "something".
No; you would have to list or concatenate every column in the query, or reorganize your database so that you have fewer columns.
SQLite has full-text search tables where you can search all columns at once, but such tables do not work efficiently with any other queries.
I could not comment on #raging-bull answer. So I had to write a new one. My problem was, that I have columns with null values and got no results because the "search string" was null.
Using coalesce I could solve that problem. Here sqlite chooses the column content, or if it is null an empty string (""). So there is an actual search string available.
SELECT *
FROM table
WHERE (coalesce(col1,"") || coalesce(col2,"") || coalesce(col3,"") || coalesce(col4,"")) LIKE '%something%')
I'm not quite sure, if I understood your question.
If you want the whole row returned, when id=searchkey, then:
select * from table where id=searchkey;
If you want to have specific columns from the row with the correct searchkey:
select col1, col2, col3 from table where id=searchkey;
If you want to search multiple columns for the "id": First narrow down which columns this could be found in - you don't want to search the whole table! Then:
select * from table where col1=searchkey or col2=searchkey or col3=searchkey;

SQL - Where clause: comparing a column with multiple values

Is there any generic command or syntax in SQL to allow one to have multiple values in a where statement without using OR?
OR just gets tedious when you have many values to choose from and you only want say half of them.
I want to return only columns that contain certain values. I am using Cache SQL, but as I said, a generic syntax might be helpful as well because most people are unfamiliar with Cache SQL. Thanks!
You should use IN:
... where column_name in ('val1', 'val2', ...);
Use the 'IN' clause.
SELECT * FROM product WHERE productid IN (1,2,3)
I believe you are looking for the IN clause.
Determines whether a specified value matches any value in a subquery or a list.