I want to query a record containing a phrase.
for example: I want the search to return the record: 'The needle in the haystack' with the search phrase 'needle haystack'
The query will work if I just have 'needle' or just 'haystack' using like% in the where clause.
Is there a way to search with the phrase 'needle haystack'?
SELECT * FROM table WHERE phrase LIKE '%needle%' AND phrase LIKE '%haystack%'
Replace phrase with LOWER(phrase) if you want the search to be case-insensitive (depends on the DB engine and other things, though).
you can also try this
select * from Suppliers where patindex(REPLACE('%' + 'YOUR SEARCH STRING' + '%',' ','%'),CompanyName) > 1
Related
Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?
Example :
SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column
From my understanding, CONTAINS can't do such kind of search.
EDIT :
I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.
You can use LIKE:
SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'
Or
SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%'
Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.
Just use the LIKE syntax together with % around the string you are looking for:
SELECT
*
FROM
table
WHERE
Column LIKE '%some random string%'
This will return all rows in the table table in which the column Column contains the text "some random string".
1) If you want to get data starting with some letter you can use % this operator like this in your where clause
WHERE
Column LIKE "%some random string"
2) If you want to get data contains any letter you can use
WHERE
Column LIKE "%some random string%"
3)if you want to get data ending with some letter you can use
WHERE
Column LIKE "some random string%"
I want to write an SQL SERVER statement that searches for the beginning of a word in a string that starts with something.
For example, if I search for 'em' on the Company record, I should get:
Emily, Inc
The Emmmy
NOT
Forget Them
Lemming, LLC
I can do this in PHP by extracting/slicing the string into an array and searching the beginning of each words.
But how I would write this query in SQL server without resorting to Stored procedures/functions?
JW's answer will work for entries where Em is at the very beginning of the field.
If you also need to retrieve values where the word is not the first in the string, try:
SELECT * FROM tableName WHERE CompanyName LIKE 'Em%' or CompanyName LIKE '% Em%'
(This is assuming all word are separated by a space.)
use LIKE
SELECT * FROM tableName WHERE CompanyName LIKE 'Em%'
Another option is CONTAINS, something like:
SELECT ... WHERE CONTAINS(CompanyName, 'Em*');
For MS Access:
SELECT * FROM Table1 WHERE Company_Name LIKE 'Word*'
For more standard DBMSs:
SELECT * FROM Table1 WHERE Company_Name LIKE 'Word%'
I am trying to create a dictionary for my website.
Searching for 'server' using FREETEXTTable & Rank DESC returns:
name server - A program or server that maps human-readable names..
server - One who serves; a waitress or waiter.
server - A tray for dishes; a salver.
4...
'server' is obviously closer to 'server' than 'name server'. How do I fix the ranking?
I can not just reverse to ASC because there are even worse matches.
Top 3 results for 'God' are 'act of God', 'Lamb of God', 'Le God'..
Edit: Sorry for any confusion. nameserver, server, server.. are in a single column called 'word' this is the column that is queried with full-text search. The definitions are in the next column 'definition' and returned as query results.
I think you can use union to solve the problem of your result ordering problem ..
like
select * from your_table_name where col_name = 'server'
union
select * from your_table_name where col_name like '%server%' order by col1,col2..
this query should give you first row with full text search and then with partial search ..
clarification ..
please note that by col_name i meant to say about the column name what you have for your words..
say your table structure is ..
dictionary-
( c_word ,
c_definition,
c_synonyms
)
then you have to modify my query as
select * from Dictionary where c_word = 'server'
union
select * from Dictionary where c_word like '%server%' order by c_definition,c_synonyms
so that this query will show first where c_word value exactly match the word 'server' followed by the partial search..
for dynamic query-- you need to replace 'server' with the variable where you are getting requested keyword for search .
I used the PATINDEX function to do this at one point. Something like the following:
SELECT
Word,
Definition
FROM
FREETEXTTABLE(Dictionary, Word, #search, 20) AS Matches
INNER JOIN Dictionary ON
Matches.Key = Dictionary.ID
ORDER BY
CASE PATINDEX('%' + #search + '%', Word)
WHEN -1 THEN 1000
ELSE PATINDEX('%' + #search + '%', Word)
END
It doesn't perform too terribly since you're using the full text index to get a smaller result set (max 20 as well, in this case). PATINDEX finds a string within an expression. If the search string doesn't exist within the expression, it returns -1. This might occur if you're also searching definitions, if your search matches on a synonym or stemmed word (eg: you search for "took" so "take" is returned), or if your search involves multiple words. The CASE statement sorts those results to the end.
I'm looking for a way to search a column of string datatype which contains a * - the problem is that the star or asterisk is a reserved symbol. The following query doesn't work properly:
select * from users where instr(pattern,"*")
How can you write an Access query to search a column for an asterisk?
You can search for reseverd charaters in Access by using square brackets:
select * from users where pattern like "*[*]*"
yay, found it out by myself:
select * from users where instr(pattern,chr(42))
Just use
select * from users where instr(pattern,"*") > 0
From Access: Instr Function
In Access, the Instr function returns
the position of the first occurrence
of a string in another string.
Use the ALIKE function because its wildcard characters do not include * e.g.
SELECT * FROM Users WHERE pattern ALIKE '%*%';
(Edit by DWF: see #onedayone's useful explanation of ALIKE)
If I have a query to return all matching entries in a DB that have "news" in the searchable column (i.e. SELECT * FROM table WHERE column LIKE %news%), and one particular row has an entry starting with "In recent World news, Somalia was invaded by ...", can I return a specific "chunk" of an SQL entry? Kind of like a teaser, if you will.
select substring(column,
CHARINDEX ('news',lower(column))-10,
20)
FROM table
WHERE column LIKE %news%
basically substring the column starting 10 characters before where the word 'news' is and continuing for 20.
Edit: You'll need to make sure that 'news' isn't in the first 10 characters and adjust the start position accordingly.
You can use substring function in a SELECT part. Something like:
SELECT SUBSTRING(column, 1,20) FROM table WHERE column LIKE %news%
This will return the first 20 characters from column column
I had the same problem, I ended up loading the whole field into C#, then re-searched the text for the search string, then selected x characters either side.
This will work fine for LIKE, but not full text queries which use FORMS OF INFLECTION because that may match "women" when you search for "woman".
If you are using MSSQL you can perform all kinds VB-like of substring functions as part of your query.