A search word is not a pattern. Try this:
select * from locations where 'Hello from Texas!' like '%' || state || '%';
or this:
select * from locations where 'Hello from Texas!' ~* ('.*' || state || '.*');
if you want Posix regexp's.
Example:
# create table locations(id integer, state text);
CREATE TABLE
# insert into locations values (1,'New York'),(2,'Texas') ;
INSERT 0 2
# select * from locations where 'Hello from Texas!' like '%' || state || '%';
id | state
----+-------
2 | Texas
(1 row)
# select * from locations where 'Hello from Texas!' ~* ('.*' || state || '.*');
id | state
----+-------
2 | Texas
(1 row)
# select * from locations where 'Greetings from you ex' like '%' || state || '%';
id | state
----+-------
(0 rows)
# select * from locations where 'Greetings from your ex' ~* ('.*' || state || '.*');
id | state
----+-------
(0 rows)
This needs some refinement or course, if you need to detect word boundaries:
# select * from locations where 'fakulos greekos metexas' ~* ('.*' || state || '.*');
id | state
----+-------
2 | Texas
If you have regex-metacharacters (See the PostgresSQL docs for as list) in your search words, then you might need to quote them first. This look a bit weird but this is what escaping always looks like:
select regexp_replace('Dont mess (with) Texas, The Lone *',E'([\(\)\*])',E'\\\\\\1','g');
The ([\(\)\*])
is the list of characters you want to escape.
However, if you never need regular expressions in your search words, then it might be easier to use a simple string searching function like strpos():
select strpos('Dont mess (with) Texas','Texas')>0;
?column?
--------
t
select strpos('Dont mess (with) Texas','Mars')>0;
?column?
--------
f
You can use upper()
if you want case insensitive compares
select strpos(upper('Dont mess (with) Texas'),upper('teXas'))>0;
?column?
--------
t