Search code examples
sqlsql-servert-sqlsql-like

An esoteric pondering regarding the lack of compatibility between % and = and <>


I am new to the world of programming but please humor me nonetheless.

I know that % works with LIKE and NOT LIKE. For example the following two queries work:

--QUERY 1
SELECT *
FROM TrumpFeccandid_Pacs
WHERE PACID NOT LIKE 'C%'

--QUERY 2
SELECT *
FROM TrumpFeccandid_Pacs
WHERE PACID LIKE 'C%'

However % does not work with = or <>. For example, the following two queries do not work:

--QUERY A
SELECT * 
FROM TrumpFeccandid_Pacs 
WHERE PACID <> 'C%'

--QUERY B
SELECT *
FROM TrumpFeccandid_Pacs
WHERE PACD = 'C%'

Why is this the case? Intuitively speaking I feel like not only should queries A and B work but Query A should be equivalent to Query 1 and Query B should be equivalent to Query 2.

These examples were using T-SQL from Sql Server 2016.


Solution

  • Image a relatively simple query like this one:

    SELECT *
    FROM A
    JOIN B ON A.Name = B.Name
    

    If = worked like LIKE, god help you if Name contains a percent or underscore!