Let's say i have the following table
user|text
1 |red 123 orange blue green
2 |red orange blue
3 |blue orange 123 red
If I wanted to pull all users whose text includes both '123' and 'blue', how would i do it? I would want to pull user 1 and 3.
SELECT *
FROM Table
WHERE text LIKE '%123%'&&'%blue%'
OR text LIKE '%blue%'&&'%123%'
Is this better solved thru using a regexp function?
Try this code:
SELECT *
FROM Table
WHERE text LIKE '%123%'
and text like '%blue%'
You need to repeat LIKE for each pattern.
SELECT *
FROM Table
WHERE text LIKE '%123%' AND text LIKE '%blue%'
You could also write it as:
WHERE text LIKE '%123%blue%' OR text LIKE '%blue%123%'
or:
WHERE text RLIKE '123.*blue|blue.*123'
However, these two solutions get exponentially large if you have to match several strings in any order. The first version is linear in the number of matches.
Related
I have a table that looks like the following:
id | cars
1 | John's Honda
2 | Andrew's red lexus
3 | James has a bmw
I need to just get the last word of the "cars" column that shows the actual "car" name
I have tried the followings but I don't get the desired output
select substr(cars, -1)
from t
the code above just shows me the last charater of the column. Later, I tried the following:
select split(cars, ' ')[offset(1)]
from t
however, I got the "Array index 1 is out of bounds (overflow)" error. Can anyone help how this can be achieved with bigquery?
Consider below simple approach
select *,
array_reverse(split(cars, ' '))[offset(0)] as brand
from your_table
if applied to sample data in your question - output is
Note: there are really many ways to accomplish your case - so anoher one would be regexp_extract(cars, r'\b(\w+)$') as brand
I have a search field in an admin system where a user can enter search criteria to find a product to edit within their shop. Some of the product names are long and similar. For example I have multiple product names with measurements in them. Here is an example:
Widget Brown Bull Edge Stone 400x500x600
Widget Brown Bull Snow Stone 700x500x300
There are about a hundred products like this, similar in name with only slight variations in either colour, size or description.
I've tried using regEx without much luck, I've also tried variations of LIKE without luck either unfortunately.
I want to be able to enter search criteria in my HTML form field and use SQL to limit the results based on the amount of search criteria I enter.
For example if I entered:
"Widget Brown 400" in the search field I can't use %like% in the where clause as it won't match that pattern.
My ideal result would be for me to enter Widget Brown 400 and the list of results would show those products that match that search criteria.
As commnented by Gordon Linoff, the best approach would be to settle for SQL-Server full text search.
Apart from that, another, less elegant and performant solution would be to use table valued function STRING_SPLIT() (available in SQL Server 2016), to turn the search words to a derived table, and then search each individual word in the text using LIKE. If all searched words are present in the text, then you have a match.
Consider:
SELECT *
FROM mytable t
WHERE NOT EXISTS (
SELECT 1
FROM STRING_SPLIT(#search, ' ') x
WHERE t.txt NOT LIKE '%' + x.value + '%'
);
Demo on DB Fiddle
CREATE TABLE mytable (id int, txt varchar(max));
INSERT INTO mytable VALUES
(1, 'Widget Brown Bull Edge Stone 400x500x600'),
(2, 'Widget Brown Bull Snow Stone 700x500x300');
DECLARE #search NVARCHAR(400) = 'Widget Brown 400';
SELECT *
FROM mytable t
WHERE NOT EXISTS (
SELECT 1
FROM STRING_SPLIT(#search, ' ') x
WHERE t.txt NOT LIKE '%' + x.value + '%'
);
id | txt
-: | :---------------------------------------
1 | Widget Brown Bull Edge Stone 400x500x600
I have a table with a column Document that is FullText Index.
Let say I have this in this table:
| ID | Document |
| 1 | WINTER SUMMER SPRING OTHER |
My requirement is to find rows that contains 'ER'.
For this I am querying like this:
SELECT TOP 100
[FullTextSearch].[Document], [FullTextSearch].[ID]
FROM
[FullTextSearch]
WHERE
CONTAINS(Document, '"*ER*"')
But this is not working.
Please suggest what should be best way to do this using FullTextSearch.
I am expecting id 1 should be returned.
You can user LIKE operator to find the value.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
Syntax,
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
This query can help to find the result.
SELECT Document,ID FROM FullTextSearch
WHERE Document LIKE '%ER%';
It's a wildcard query...This should work.
SELECT TOP 100
[FullTextSearch].[Document], [FullTextSearch].[ID]
FROM
[FullTextSearch]
WHERE
Document like '%ER%'
========OR=============
SELECT TOP 100
[FullTextSearch].[Document], [FullTextSearch].[ID]
FROM
[FullTextSearch]
WHERE
CONTAINS(Document, '%ER%')
I'm trying to create a simple search page on my site but finding it difficult to get full text search working as I would expect it to word.
Example search:
select *
from Movies
where contains(Name, '"Movie*" and "2*"')
Example data:
Id Name
------------------
1 MyMovie
2 MyMovie Part 2
3 MyMovie Part 3
4 MyMovie X2
5 Other Movie
Searches like "movie*" return no results since the term is in the middle of a work.
Searches like "MyMovie*" and "2*" only return MyMovie Part 2 and not MyMovie Part X2
It seems like I could just hack together a dynamic SQL query that will just
and name like '%movie%' and name like '%x2%' and it would work better than full text search which seems odd since it's a large part of SQL but doesn't seem to work as good as a simple like usage.
I've turned off my stop list so the number and single character results appear but it just doesn't seem to work well for what I'm doing which seems rather basic.
select
*
from Movies
where
name like ('%Movie%')
or name like ('%2%')
;
select * from Movies where freetext(Name, ' "Movie*" or "2*" ')
I have an stored procedure which returns result based on what text i send as search string
I have 4 records with text
1 egg whole raw
2 egg cooked
3 egg fried omelet
4 raw egg and tomatoes
I want to perform a like search such that if i specify ' egg ra ' as my search string in my search text box it should filter and return two rows as result
1 egg whole raw
2 raw egg and tomatoes
how should my like query be which will return me this result ?
select * from your_table
where text_column like '%egg%'
and text_column like '%ra%'
But this won't make use of indexes and can be slow.
select * from your_table WHERE FREETEXT (text_column, "egg ra");
You have to enable full free text searching in MSSQL to do the above, below are the other options used fro searching for a text.
CONTAINS (Transact-SQL)
CONTAINSTABLE (Transact-SQL)
FREETEXT (Transact-SQL)
FREETEXTTABLE (Transact-SQL)
Read this for more info - http://msdn.microsoft.com/en-us/library/ms142547%28v=sql.105%29.aspx