I want to search for the occurrence of words in a database row. I use SQLite as a database. The search needs to be case-insensitive.
Let's say row x is Hello. Today is Monday and I want to search for Hello Monday Bye and still return the row x because the Hello and Monday exist in the row.
I used Like operator %Hello%Tuesday%Bye% but this defiantly doesn't work and I can't use Glob because it's not case-insensitive. Any suggestion, how can I do the search?
Also, the order shouldn't matter and Monday Hello should return a row as well
You can extend SQLite with regular expressions and then use:
where col regexp replace($searchfor, ' ', '|')
This will return any time there is a match with any of the values.
First create a recursive CTE to split the string 'Hello Monday Bye' which contains the words you search for.
Then join it to the table:
with cte as (
select null word, 'Hello Monday Bye' || ' ' rest
union all
select substr(rest, 1, instr(rest, ' ') - 1),
substr(rest, instr(rest, ' ') + 1)
from cte
where length(rest) > 0
)
select distinct t.*
from tablename t inner join cte c
on t.col like '%' || c.word || '%'
See the demo.
I have a table with CSV values as column. I want use that column in where clause to compare subset of CSV is present or not. For example Table has values like
1| 'A,B,C,D,E'
Query:
select id from tab where csv_column contains 'A,C';
This query should return 1.
How to achieve this in SQL?
You can handle this using LIKE, making sure to search for the three types of pattern for each letter/substring which you intend to match:
SELECT id
FROM yourTable
WHERE (csv_column LIKE 'A,%' OR csv_column LIKE '%,A,%' OR csv_column LIKE '%,A')
AND
(csv_column LIKE 'C,%' OR csv_column LIKE '%,C,%' OR csv_column LIKE '%,C')
Note that match for the substring A means that either A,, ,A, or ,A appears in the CSV column.
We could also write a structurally similar query using INSTR() in place of LIKE, which might even give a peformance boost over using wildcards.
there's probably something funky you can do with regular expressions but in simple terms... if A and C will always be in that order
csv_column LIKE '%A%C%'
otherwise
(csv_column LIKE '%A%' AND csv_column LIKE '%C%' )
If you don't want to edit your search string, this could be a way:
select *
from yourTable
where csv like '%' || replace('A,C', ',', '%') || '%'
For example:
with yourTable(id, csv) as (
select 1, 'A,B,C,D,E' from dual union all
select 2, 'A,C,D,E' from dual union all
select 3, 'B,C,D,E' from dual
)
select *
from yourTable
where csv like '%' || replace('A,C', ',', '%') || '%'
gives:
ID CSV
---------- ---------
1 A,B,C,D,E
2 A,C,D,E
Consider that this will only work if the characters in the search string have the same order of the CSV column; for example:
with yourTable(id, csv) as (
select 1, 'C,A,B' from dual
)
select *
from yourTable
here csv like '%' || replace('A,C', ',', '%') || '%'
will give no results
Why not store the values as separate columns, and then use simple predicate filtering?
This is very much like PostgreSQL wildcard LIKE for any of a list of words, except instead of wanting to match on a static list of words, I want to match on a list of words returned by a subquery.
Something like this:
SELECT *
FROM people
WHERE name ~* (SELECT concat(last_name, ', ', first_name)
FROM other_people) + wildcard in this direction
select *
from people
where name like any (
select concat(last_name, ', ', first_name, '%')
from other_people
)
I'm struggling for a like operator which works for below example
Words could be
MS004 -- GTER
MS006 -- ATLT
MS009 -- STRR
MS014 -- GTEE
MS015 -- ATLT
What would be the like operator in Sql Server for pulling data which will contain words like ms004 and ATLT or any other combination like above.
I tried using multiple like for example
where column like '%ms004 | atl%'
but it didn't work.
EDIT
Result should be combination of both words only.
Seems you are looking for this.
`where column like '%ms004%' or column like '%atl%'`
or this
`where column like '%ms004%atl%'
;WITH LikeCond1 as (
SELECT 'MS004' as L1 UNION
SELECT 'MS006' UNION
SELECT 'MS009' UNION
SELECT 'MS014' UNION
SELECT 'MS015')
, LikeCond2 as (
SELECT 'GTER' as L2 UNION
SELECT 'ATLT' UNION
SELECT 'STRR' UNION
SELECT 'GTEE' UNION
SELECT 'ATLT'
)
SELECT TableName.*
FROM LikeCond1
CROSS JOIN LikeCond2
INNER JOIN TableName ON TableName.Column like '%' + LikeCond1.L1 + '%'
AND TableName.Column like '%' + LikeCond2.L2 + '%'
Try like this
select .....from table where columnname like '%ms004%' or columnname like '%atl%'
I have a column in SQL table which would have data like this:
"College: Queenstown College" or "University: University of Queensland"
Text before the ":" could be different. So, how can i select only the text before the ":" from the column and text after the ":(space)"?
You should probably consider putting your table into first normal form rather than storing 2 pieces of data in the same column...
;with yourtable as
(
select 'College: Queenstown College' as col UNION ALL
select 'University: University of Queensland'
)
select
left(col,charindex(': ',col)-1) AS InstitutionType,
substring(col, charindex(': ',col)+2, len(col)) AS InstitutionName
from yourtable
Using the charindex and substring functions:
substring(theField, 1, charindex(':', theField) - 1)