SQL find word in string - sql

I have a string like that format:
'%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%'
and i need to know is this string contains a word from column mName in table1:
In this case I need the output to be galaxy

I do not know what database do you use but this will work in Mysql and Oracle:
select *
from table1
where instr('%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%', mname) <> 0;
I have created a table called test
You use instr function to find a value from a column mName in the string if it exists then you show it.
Here is a DEMO
Or as forpas suggested in his comment you can use like:
select * from table1
where '%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%'
like '%'||mname||'%'

Related

SQL search for string containing characters

I have a string "abc", and I want to search, in a SQL table, for the values that contain either a, b or c.
This is what I have right now:
Select * from TABLE where NAME like "abc" ;
This didn't work, but I know if I try something like
where Name like "a" or Name like "b" or ....
it will work.
Is there an easier way to do this?
Since I don't want to separate my string into characters.
You can use regular expression for this.
Have a look at the following :
Select * from TABLE where NAME REGEXP "[abc]";
select * from ab where name REGEXP '[abc]'
This will do
Select * from TABLE where NAME like "%abc%" ;
For more check information here http://www.techonthenet.com/sql/like.php

Simple Select statement does not return any result

I have one database table name test123 and having column name. And it contains the data like 'nir,kal,man' Now, when i am querying the table with select statement as per below :
select * from test123 where name = 'nir,kal,man';
But this will not return any result...Why this happened.? How i have to write query so that will return the result?
I am using Sql server 2008.
Thanks...!
= operator returns exact match, so if your cell contain data "like" that you need to use LIKE operator:
select * from test123 where name like '%nir,kal,man%'
where % will be replaced with any set of characters.
Also check that you're targeting correct database by using full name
select * from yourdb.dbo.test123 where....
if Nir is in first row Kal in 2nd row and man is in 3rd row then you should write query like this
select * from test123 where name in ('nir','kal','man')

Oracle select all rows using the contains keyword

I'm looking for a character that I can use in an Oracle contains to get ALL results.
If I search for the string "test" in a title column I use this statement:
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">test</textquery></query>') > 0
With this statement I get the rows which have the "test"-string included in title-column.
BUT: Is there any way to use contains and select ALL rows?
Would this work for you?
select * from my_table
where title like
'<query><textquery grammar="CTXCAT">%</textquery></query>'
This uses the LIKE syntax and the % wildcard to achieve what I think you want.
The documentation says wildcards with the Oracle Text CONTAINS() predicate are % and _ just like the LIKE predicate.
Does the following work? I don't have an instance of Oracle handy to test.
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">%</textquery></query>') > 0

Why does MySQL always return no data for this query?

I have a database in which a few fields are set as index. I don't know if this has to do with the problem or not, but when creating a query I get no results. Here is how I have things set in the database. The database has 90,000 records and this table is set like:
| fieldA | fieldB | fieldC | fieldD |
fieldA = index - CHAR
fieldB = index - CHAR
fieldC = index - CHAR
fieldD = index - VARCHAR
If I do:
SELECT *
FROM tableA
WHERE 'fieldD'='A LONG STRING WITH SYMBOLS AND CHARCTERS';
I get 0 results and I know that the value for fieldD exist and is there.
What am I doing wrong?
It looks to me like your problem is you're enclosing your column names with single quotes. This is causing MySQL to treat it as a string instead of a column name, so you are literally comparing the string 'fieldA' with long string. Remove the quotes from around the column name and you should be good to go.
SELECT * FROM tableA WHERE fieldD='A LONG STRING WITH SYMBOLS AND CHARCTERS';
There could be a problem with character encoding here. I would try this query and see what happens:
SELECT *
FROM tableA
WHERE 'fieldD' like '%A LONG STRING WITH SYMBOLS AND CHARCTERS%';
Also, I noticed a typo on the word 'CHARACTERS'.
Throw away apostrophes around field name, You may use them but apostrophes around field names are opposite = "ยด"
SELECT * FROM tableA WHERE fieldD='A LONG STRING WITH SYMBOLS AND CHARCTERS'
Not sure if this was a typo in your query above, but fieldD should not be in single quotes.
BACKTICKS instead of single quotes
SELECT * FROM tableA WHERE `fieldD`='A LONG STRING WITH SYMBOLS AND CHARCTERS';
The backticks key is to the left of the 1 key
I don't know why but the following query did the trick. If any of you can explain why will be appreciated:
SELECT *
FROM tableA
WHERE completo LIKE (
'1008017 P MT/75 FRONT 52H%'
)
LIMIT 0 , 30
I had to place a %' at the end of the string to work
the column does not have the data in it you think it does. try this:
first run this query:
select hex('100/80R16 M A39 50S')
then run this query:
select fieldD, hex(fieldD) from tableA
then compare the results from the first query to the appropriate row from the second query. i think you will find the hex() representations do not match.
if you can't tell why they don't match from the hex() output, post it here and i'll help you figure out why it's not working.

SQL select using condition that a column starts with a certain string

I am using sql.
How to select all items from a table.
where a column (varchar) path is start with a certain string : "stringABC"
select * from ATable where path like 'stringABC';
The above sql is wrong, can any one help me to fix it?
Many thanks!
select *
from ATable
where path like 'stringABC%'
select * from ATAble where path like 'string%'