Search code examples
sqlsql-serverfunctionsql-likeazure-sql-managed-instance

SQL 'Like' function - Clarification


In the below query, the result is 1. My doubt is why the first condition does not satisfy?

I thought that both the cases which are mere vice versa should have been same, so shouldn't the first case have been satisfied?

What am I getting wrong here?

Query:

select case when '%G24F_T15%' like '%TK_G24F_T15_DITPG204A15WA%'
then 0
when '%TK_G24F_T15_DITPG204A15WA%' like '%G24F_T15%'
then 1
end as abc

Solution

  • Like is not symmetric, so % signs on the left hand side are taken literally. That is, they are seen as % signs rather than pattern matching commands.

    Syntax from the help page:

    -- Syntax for SQL Server and Azure SQL Database  
    
    match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]  
    

    The two sides are not the same - only the right hand side can have pattern matching symbols.