I want to use the like command in a query to find specific results in Access. In the field that I want to utilize the like command has the wildcard (*) command in that field, which I want to search by. Is there a way around this? Example below:
SELECT *
FROM TABLE_NAME
WHERE FIELD_NAME LIKE '*'
Assuming you want to find columns that actually contain an asterisk (*) via the LIKE operator, here is an idea to try (see http://office.microsoft.com/en-us/access-help/using-wildcard-characters-in-string-comparisons-HP001032284.aspx )
SELECT *
FROM TABLE_NAME
WHERE FIELD_NAME LIKE '[*]'
The square brackets indicate that it's a literal asterisk to match, and not a wildcard.
* is not a wildcard in sql server like. So you can use:
SELECT *
FROM TABLE_NAME
WHERE FIELD_NAME LIKE '%*%' ;
If you want to search for actual wildcard %, you can change the wilcard to something else say &
SELECT *
FROM TABLE_NAME
WHERE FIELD_NAME LIKE '&%&' ESCAPE '&';
Related
I have tags column in database separated by commas like that: "green, big, strong". I know it's not the best way to store tags in database, and I will change it later, but right now I really need to find out how to get row that has special tag.
For example, in the first row column tags is "green, big, strong". And in the second row is "white, big, strongy".
When I use
"SELECT * FROM tablename WHERE tags LIKE '%strong%'"
it returns the first and the second column (because of strongy), but I need only the first one
I would do this as:
SELECT *
FROM tablename
WHERE ',' || tags || ',' LIKE '%,strong,%';
That is, just add the delimiters so the search is not ambiguous.
Limit output to 1 like so:
SELECT * FROM tablename WHERE tags LIKE '%strong%' LIMIT 1
Could also sort by specific column such as ID, like so:
SELECT * FROM tablename WHERE tags LIKE '%strong%' ORDER BY column_name LIMIT 1
Brackets specify a range when used with LIKE. You can use the ESCAPE keyword.
WHERE mycol LIKE '%\[126\]%' ESCAPE '\';
Of course if you are trying to match an exact string, you don't need LIKE, or you can drop the % characters and LIKE will behave like = (this makes it flexible to pass in wildcards or exact matches to a parameter).
I know you can use LIKE for a select statement to search for a string and then use wildcards to match that string.. ex:
Select * from table where first_name LIKE '%r%';
Would return names such as robert, roland, craig, etc.
I'm curious how you would search for an actual wildcard in the string such as the %:
Select * from table where values LIKE '% % %';
(the middle % being what you would be looking for) (obviously this is not the correct way to do it which is why I'm asking).
How about
LIKE '%\%%' ESCAPE '\'
http://docs.oracle.com/cd/B12037_01/server.101/b10759/conditions016.htm
Just LIKE '%%' will be even easier.
In phpMyAdmin, there is a search in database feature by which I can input a word and find it in any table(s).
How to implement it by SQL statement? I know the LIKE operation, but it's syntax is:
WHERE column_name LIKE pattern
How to search in all columns? Any how to specify it's a exact keyword or regular express?
SELECT * FROM your_table_name WHERE your_column_name LIKE 'search_box_text';
Where search_box_text is what you enter in the search. It will also say in the result page what kind of query it made. The same query with regular expressions is:
SELECT * FROM your_table_name WHERE your_column_name REGEXP 'search_box_text';
Remember that the wildcard in mysql is %. Eg. "LIKE '%partial_search_text%'
If you want to search in multiple columns, you can check which columns are in table with:
DESCRIBE TABLE your_table_name;
Or if you already know your columns:
SELECT * FROM your_table_name
WHERE your_column_1 LIKE '%search%'
AND your_column_2 LIKE '%search%'
AND your_column_3 LIKE '%search%';
I have record in table like 'abc 100% text'.
I want to search all the records that contain 100%.
What will be LIKE query?
SELECT * FROM TABLE where ColName LIKE '100%%'
above query returns wrong results.
Thanks.
SELECT * FROM TABLE where ColName LIKE '%100[%]%'
Have a look at Using Wildcard Characters As Literals
You can use the wildcard pattern matching characters as literal characters. To use a wildcard character as a literal character, enclose the wildcard character in brackets.
SELECT columns FROM table WHERE column LIKE '%[%]%'
or
SELECT columns FROM table WHERE column LIKE '%\%%' ESCAPE '\'
as described in http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
Additionally, you can use escape charaters...
LIKE '%100^%%' ESCAPE '^'
http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
Use a backslash to escape:
SELECT * FROM TABLE where ColName LIKE '%100\%%';
You may need to try this:
SELECT * FROM TABLE where ColName LIKE '%100\%%' ESCAPE '\';
Well given I have a value I want to check for potential matches in a database (in one varchar field) so I write something like:
SELECT * FROM table WHERE column LIKE "%value%"
Which will work if the value is something like "test" and the column has a value of "this is a test" however if it is reversed then I will not get a match I have tried things along the lines of:
SELECT * FROM table WHERE CONCAT("%",column,"%") LIKE "value"
but don't know exactly how to phrase this to Google to get a response I need, please help!
You can reverse a like statement. Just using the same syntax as a regular like query:
select
*
from
table
where
'value' like concat('%', column, '%')
Of course, if you felt wild and crazy, you could also use instr:
select * from table where instr('value', column) > 0
I don't know which one is faster, since I don't have a MySQL instance to test against, but it's worth trying both to see which wins.
SELECT *
FROM table
WHERE 'value' LIKE CONCAT('%', column, '%')