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
Related
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
I have this query in access but it doesn't return any result when I use the Like function. It does return result when I set it to equal to.
Select * from myTable where name like "*Al#pp*"
note that the # sign is part of the spelling to lookup and not a wildcard, but I think access is taking it as a wildcard.
Since # is not supposed to be a wildcard then you have to escape it. Try:
Select * from myTable where name like "*Al[#]pp*"
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
When I search in SQL Server for a column with a structure like this:
1-) 0.07.00.00-456.1/2268
2-) 20.07.00.00-0000003/8
with this query:
SELECT *
FROM table_name tn
WHERE ((CONTAINS(tn.coulmn_name, '*8*')))
I expect it returns 1 and 2 rows but it only return which only come after slash 20.07.00.00-0000003/8.
I am using the syntax blabla, it must return with all the results which contains 8.
Instead of using contains you could use like to do what you want, you would just have to use wildcards see below:
Select 1
WHERE ('0.07.00.00-456.1/2268' LIKE '%8%'
AND '20.07.00.00-0000003/8' LIKE '%8%')
Your Code :
SELECT *
FROM table_name tn
WHERE tn.coulmn_name LIKE '%8%'
link for reading : https://msdn.microsoft.com/en-us/library/ms179859.aspx
Hope that helps!
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%'