Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IIf(xxx,yyy,zzz) in SQL 1

Status
Not open for further replies.

christheprogrammer

Programmer
Joined
Jul 10, 2000
Messages
258
Location
CA
Hiya, I have a query which was built in ACCESS and it contains:

SELECT
[table1].[x] AS CHOICE_VALUE,
IIf([table1].[x] = 2, [table1].[y], 1) AS CHOICE
FROM [table1]

The IIf statement means:

If([table1].[x] = 2){
SELECT [table1].[y] AS CHOICE
}else{
SELECT 1 AS CHOICE
}

How do I do this in (TRANSACT) SQL using only one query? I know it can be done by using 2 queries, one of which is an UPDATE query but I would like to do it in one query.. TIA! Chris says: "It's time for a beer."
 
Use the Case function in Transact-SQL.

SELECT
[table1].[x] AS CHOICE_VALUE,
Case When [table1].[x] = 2
Then [table1].[y]
Else 1 End AS CHOICE
FROM [table1]

I recommend posting Transact-SQL questions in forum181 - Microsoft SQL Server or forum187 - Sybase: Adaptive Server. Use the forum for the product you use. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Thanks Terry,
It worked like a charm...

Cheers Chris says: "It's time for a beer."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top