Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IIF equivalent

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2011-02-10 : 08:24:41
I know there is no IIF in SQL but what equivalent can I use in a view

eg

IIF Emp = "Contractor" then 1 else 0

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-02-10 : 08:44:32
CASE
WHEN Emp = 'Contractor' THEN 1
WHEN Emp = ... THEN ...
ELSE 0
END

or also possible
CASE Emp
WHEN 'Contractor' THEN 1
WHEN ... THEN ...
ELSE ...
END


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2011-02-10 : 08:53:58
Sorry to be so dumb - but how do I add this to the view ? Do I put the whole lot in the column field (it's in design view) or do I just edit it into the SQL ?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-02-10 : 08:59:41
The view designer doesn't support CASE constructor.

ALTER VIEW dbo.MyViewNameHere
AS

SELECT Col1,
CASE
WHEN Emp = '1' THEN 'S'
WHEN Emp = '2' THEN 'E'
ELSE 'T'
END AS Manager,
Col2
FROM dbo.Table1


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2011-02-10 : 09:05:26
Sorted it - thanks. I could enter it in the designer - in the column (SQL SERVER 2008)
Go to Top of Page
   

- Advertisement -