Like operator ( [ ] ) in SQL

1. Matching Specific Characters in Product IDs

Query - Select * From [Product_data_abu] Where ProductCode Like '[1234]%'

How it works - 

  • [1234]% → Matches values where the first character is 1, 2, 3, or 4.

  • It excludes 5D67 and 7F90 (since their first digit isn’t in [1234]).

Source DE :-


Query 



Target DE after query :-


2.Matching Specific Numbers in Comma-Separated Lists



Query - Select * From [EcommerceUpsellDE_Abu] Where RecommendedProducts Like '[1-4]' OR RecommendedProducts Like '[1-4],%' 
OR RecommendedProducts Like '%,[1-4],%' OR RecommendedProducts Like '%,[1-4]'


How it works - 
  • Different positions in the list are checked:

    • [1-4] → Matches if the first value is 1, 2, 3, or 4.

    • [1-4],% → Matches if the first value is followed by more values.

    • %,[1-4],% → Matches if 1, 2, 3, or 4 appears anywhere in the middle.

    • %,[1-4] → Matches if 1, 2, 3, or 4 is the last value.

💡 Why is this important?
Without checking different positions, SFMC SQL wouldn’t find values inside the comma-separated list.




Source DE 


Query 




Target DE after query 


3. Using [] with Letters

Query - Select * From [Customers_Abu] Where CustomerName Like '[ABC}%'

How it works - 

  • [ABC]% → Matches names starting with A, B, or C.

  • It excludes David and Emily since their first letters aren’t in [ABC].



Source DE


Query







Target DE after query



4. Using [] to Escape Special Characters


Query -
Select * From [Product_Abu] Where Discount Like '%[%]'

How it works
  • [%] tells SQL to treat % as a normal character, instead of a wildcard.

  • Without [ ], SQL would treat % as "match any characters", leading to incorrect results.


Source DE


Query


Target DE after query











Comments

Popular posts from this blog

Use Cases of Standard Data Extensions

Data retention policy