Search code examples
sqlsql-serveroperatorswildcardsql-server-2014

SQL : Confused with WildCard operators


what is difference between these two sql statements

  1-  select * from tblperson where name not like '[^AKG]%';

  2-  select * from tblperson where name like '[AKG]%';

showing same results: letter starting from a,k,g


Solution

  • like '[^AKG]% -- This gets you rows where the first character of name is not A,K or G. ^ matches any single character not in the specified set or a specified range of characters. There is one more negation not. So when you say name not like '[^AKG]%' you get rows where the first character of name is A,K or G.

    name like '[AKG]% -- you get rows where the first character of name is A,K or G.

    The wildcard character [] matches any character in a specified range or a set of characters. In your case it is a set of characters.

    So both the conditions are equivalent.