What happened to this T-SQL statement?

Why does this throw an equal sign error?

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

Error:

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

+4
source share
3 answers

IIF is a feature of SQL Server 2012, you will need to use CASE

 SELECT CASE SUBSTRING('A1234', 1, 1) WHEN 'A' THEN 'TRUE' ELSE 'FALSE' END 
+6
source

You must replace IIF with CASE, also TRUE and FALSE do not exist in SQL Server, you can use VARCHAR or BIT

 select CASE WHEN SUBSTRING('A1234', 1, 1) = 'A' THEN 'TRUE' ELSE 'FALSE' END as IsAustraliaUser 
+1
source

I do not think SQL supports this IIF syntax (if you are not using 2012), replace it with a case.

 SELECT CASE WHEN ( SUBSTRING('A1234', 1, 1) = 'A' ) THEN 'TRUE' ELSE 'FALSE' END AS IsAustraliaUser 
+1
source

Source: https://habr.com/ru/post/1502069/


All Articles