Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong this T-SQL statement?

Why is this throwing an error on the equal sign?

select IIF((SUBSTRING('A1234', 1, 1) = 'A'), TRUE, FALSE) as IsAustraliaUser

Error:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.

like image 991
JCleveland Avatar asked Feb 16 '23 07:02

JCleveland


2 Answers

IIF is a SQL Server 2012 feature, you'll need to use CASE

SELECT CASE SUBSTRING('A1234', 1, 1)
       WHEN 'A' THEN 'TRUE'
       ELSE 'FALSE'
       END
like image 117
Darren Avatar answered Feb 19 '23 20:02

Darren


You should replace IIF with CASE, also TRUE and FALSE don't exists in SQL Server, you can use VARCHAR or BIT

select CASE WHEN SUBSTRING('A1234', 1, 1) = 'A' THEN 'TRUE' ELSE 'FALSE' END as IsAustraliaUser
like image 34
EricZ Avatar answered Feb 19 '23 21:02

EricZ