Sunday, February 19, 2012

IIF

Is there in TransactSQL something like the IIF in Access?
Example
SELECT MyTable.MainID,
IIf([Status]=1 Or [status]=2 Or [status]=7 Or [status]=9,1,0)
AS LowStatus, StatusDate
FROM MyTable;
ThanksSELECT MyTable.MainID,
CASE WHEN [Status]=1 Or [status]=2 Or [status]=7 Or
[status]=9 THEN 1 ELSE 0 END AS LowStatus, StatusDate
FROM MyTable;|||You need to make use of CASE function which is available in SQL Server.
For example:
-- Table structure
Create Table Tbl1
(
Sno int identity,
fname varchar(30) not null
)
Go
-- SAmple data --
Insert into Tbl1 values (fname) values ('r')
Insert into Tbl1 values (fname) values ('a')
Insert into Tbl1 values (fname) values ('b')
Go
-- Sample Query using CASE function
Select fname,
Case fname
When 'r' then 'Rocky'
When 'a' then 'Alex'
End
From tbl1
Hope this helps!
Best Regards
Vadivel
http://vadivel.blogspot.com
"Bart Steur" wrote:

> Is there in TransactSQL something like the IIF in Access?
> Example
> SELECT MyTable.MainID,
> IIf([Status]=1 Or [status]=2 Or [status]=7 Or [status]=9,1,0)
> AS LowStatus, StatusDate
> FROM MyTable;
> Thanks
>
>|||Transact-SQL does not have an IIF expression, but you can use the CASE
expression. See Example C in the CASE reference topic:
http://msdn2.microsoft.com/en-us/library/ms181765(SQL.90).aspx
This example was added to the April 2006 update to the SQL Server 2005 Books
Online. You can update your local copy of Books Online using the download
at:
http://www.microsoft.com/technet/pr...oads/books.mspx
Alan Brewer [MSFT]
Content Architect, SQL Server Documentation Team
Download the latest Books Online update:
http://www.microsoft.com/technet/pr...oads/books.mspx
This posting is provided "AS IS" with no warranties, and confers no rights.

No comments:

Post a Comment