0

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
Venkat
  • 47
  • 7
  • Why do you expect the `like` operator to be symmetric? *match_expression* and *pattern* have very different descriptions in the [documentation](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15) – Damien_The_Unbeliever Apr 07 '20 at 08:56
  • `LIKE` will search for the right side match on the left side, and not the opposite. You can use wildcard only in the right side, and they will be taken as a literal string if they appears in the left side – Ilyes Apr 07 '20 at 08:59

1 Answers1

2

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.

codeulike
  • 22,514
  • 29
  • 120
  • 167