SQL: How to perform string does not equal

Dan Ciborowski - MSFT picture Dan Ciborowski - MSFT · May 1, 2013 · Viewed 236.8k times · Source

I have the following query

SELECT * FROM table
WHERE tester <> 'username';

I am expecting this to return all the results where tester is not the string username, But this not working. I think I am looking for the inverse of the Like operator but I am not sure? In my searches I have found solutions for numbers (that's where i got <> from), but this seems to not be working with strings.

Answer

Gordon Linoff picture Gordon Linoff · May 1, 2013

Your where clause will return all rows where tester does not match username AND where tester is not null.

If you want to include NULLs, try:

where tester <> 'username' or tester is null

If you are looking for strings that do not contain the word "username" as a substring, then like can be used:

where tester not like '%username%'