I want to do the following (works in MySQL, etc.) in T-SQL:
IF(name LIKE '%Amazon%', TRUE, FALSE) AS 'is_amazon'
So I tried this, but it errors on the
IIF(name LIKE '%Amazon%', 1, 0) AS 'is_amazon'
The error is:
Parse error at line: 25, column: 17: Incorrect syntax near ‘LIKE’.
Column 17 in the real world usage is the
LIKE for what it’s worth.
The Microsoft T-SQL documentation says that:
LIKEis a "logical operator" returning a boolean
so why does this not work? How can I make this work in T-SQL?
Here are two options. You can cast as BIT if desired
Declare @YourTable table (SomeCol varchar(50)) Insert Into @YourTable values ('Some Name') ,('Some Amazon Vendor') Select SomeCol ,IsAmazon1 = sign(charindex('Amazon',SomeCol)) ,IsAmazon2 = case when SomeCol like '%Amazon%' then 1 else 0 end From @YourTable
SomeCol IsAmazon1 IsAmazon2 Some Name 0 0 Some Amazon Vendor 1 1