SQL - search by beginning of a word - sql

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%'

Related

How to use LIKE in WHERE clause to get first 5 characters of variable?

I have a variable varchar that always takes in 10 digits. How can I use the LIKE operator to find/use only the first 5 digits of the variable?
my query:
variable IN VARCHAR2
SELECT * FROM items WHERE name LIKE SUBSTRING(variable, 1, 5)
... WHERE name LIKE '12345%'
will match any string that starts 12345. the '%' is a wildcard. You can also use the wildcard to match anywhere in the string: ... WHERE name LIKE '%12345%' will match a string with 12345 anywhere within it.
Edit for completeness: WHERE name LIKE '%12345' will match any string that ends with those five characters.
Try this:
SELECT * FROM items WHERE name LIKE (SUBSTRING(variable, 1, 5) + '%')
I guess you can use LEFT() like this:
SELECT * FROM items WHERE LEFT(name,5)=LEFT(variable,5);
Or if you you want to use LIKE with a wildcard, you can do this:
SELECT * FROM items WHERE name LIKE CONCAT(LEFT(variable,5),'%')
A few more example in the Demo fiddle
Edit: The above solution is for MySQL/MariaDB because earlier the tag of this question have MySQL but it's also my fault for not recognizing OP description of the datatype VARCHAR2. I might as well just post a suggestion related to the rdbms.
So, my first suggestion there using LEFT() however Oracle don't have that function, therefore:
SELECT * FROM items WHERE SUBSTR(name,1,5)=SUBSTR(variable,1,5);
or using concatenation operator
SELECT * FROM items WHERE name LIKE SUBSTR(variable,1,5)||'%'
Demo fiddle

SQL Server full text search few words at a time

I understand that in order to find records that contain more than one word, I need to use AND between them, like this:
select *
from table1
where contains(name , '"bob" AND "marly"')
The problem is that my user doesn't type "bob" AND "marly", he types bob marly.
Before I start parsing this string and splitting it, is there a cleaner way to do this?
I'm using SQL Server 2012.
Thanks.
The below query will check if the string contains bob marly.
SELECT * FROM table1 WHERE name LIKE '%'+'bob'+'%'+'marly'+'%'

SQL Wildcard in Access: "%" and "_" don't works

I tryed some wildcards in Access in Where statement, but they don't works. For example:
This query SELECT staff.* FROM staff;returns:
I tryed to do a query with wildcard SELECT staff.* FROM staff WHERE (staff.s_name LIKE "A%");
but it returns an empty table:
What is the reason? My wildcard doesn't work
(s_name is the second column)
(look that "firstname" is the tag of "s_name" only for the view)
Wildcard character in Access is *, not % unlike in SQL Server.
See MSDN for details.
No, no, no, use '*', not '%'. Or, use 'Like'.
http://www.techrepublic.com/article/10-tips-for-using-wildcard-characters-in-microsoft-access-criteria-expressions/
https://www.techonthenet.com/access/queries/like.php
https://www.techonthenet.com/access/queries/like2007.php
For instance:
Like 'm*'
Result: all values that start with m
Like 'm'
Result: all values that contain m
Like '*m'
Result: all values that end with m

Trying to use Wildcards in SQLite Browser but its not working

i want to display all the names that start with S.
so i tried writing this:
select
*
from
User
where
name = 'S%';
0 rows returned.
select
*
from
User
where
name like 'S%';
If you want to read up on how wildcards work: http://www.w3schools.com/sql/sql_wildcards.asp

MS Access: searching a column for a star/asterisk

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)