Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to return rows where column does not contain a certain substring

Is there something I can put in my WHERE clause to only select fields from a column where that field does not contain a certain string. In this case, I am looking through to make sure the codes in this particular field do not have a "cs" in it. The code could be something like cs023 or bg425, just to give you a little bit more of an idea what I'm looking to do.

like image 290
Hani Honey Avatar asked Sep 22 '11 16:09

Hani Honey


People also ask

Can we SELECT column which is not part of group by?

In MySQL, when you try to select a column that isn't used in the GROUP BY clause, or in an aggregate function inside the statement, it is not a valid statement according to SQL standard and will cause an error.

How do I find a particular substring in SQL?

Use the SUBSTRING() function. The first argument is the string or the column name. The second argument is the index of the character at which the substring should begin. The third argument is the length of the substring.

Can we use substring in WHERE clause in SQL?

The SUBSTRING SQL function is very useful when you want to make sure that the string values returned from a query will be restricted to a certain length. In the following example, using the 'firstname' column, the last two characters are matched with the word 'on' using the SQL SUBSTRING function in the where clause.


1 Answers

You can use

WHERE [column] NOT LIKE '%cs%'

Replace [column] with your column name.

like image 51
Wil Avatar answered Sep 28 '22 17:09

Wil