user6731260
user6731260

Reputation: 105

Checking if string is in table

Hi I have table called mytable and 1 column first_name , how can I check if 'John' is in this table and return the result as true/false.

Upvotes: 0

Views: 608

Answers (3)

Viktor Seifert
Viktor Seifert

Reputation: 636

You can utilize a EXISTS-query:

select exists (select * from mytable where mytable.first_name = 'John')

The above query will return a boolean which will be true if the sub-query inside the braces returns any rows at all; the boolean will false if the sub-query return no rows.

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522074

SELECT CASE WHEN COUNT(*) > 0 THEN 'true' ELSE 'false' END
FROM mytable
WHERE first_name LIKE '%John%'

Upvotes: 0

user6146785
user6146785

Reputation:

SELECT count(*) FROM mytable WHERE first_name LIKE 'John';

returns number of occurrences. If there is no John in table, returns zero.

Upvotes: 0

Related Questions