SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Other Forums
 MS Access
 writing IIF formula in SQL Server
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

forrestgump
Starting Member

18 Posts

Posted - 02/04/2013 :  09:52:25  Show Profile  Reply with Quote
Hi there,

I am trying to write an IIF formula in sql server to create an extra column but I am new to sql server. The formula in the access sql is written as:

IIf([UKUS]= NOT Null,[UKUS],IIf([MoW] = NOT Null,[MoW])) AS Made,

However, this is not accepted in SQL server. How would I write this in SQL server?

Regards,

Forrest

James K
Flowing Fount of Yak Knowledge

3588 Posts

Posted - 02/04/2013 :  10:01:44  Show Profile  Reply with Quote
An exact translation of your expression in a T-SQL SELECT would be
CASE
	WHEN [UKUS] IS NOT NULL THEN [UKUS]
	WHEN [MoW] IS NOT NULL THEN [MoW]
	ELSE [Mow]
END AS Made
However, you don't need the second WHEN so it can be simplified to:
CASE
	WHEN [UKUS] IS NOT NULL THEN [UKUS]
	ELSE [Mow]
END AS Made
Go to Top of Page

forrestgump
Starting Member

18 Posts

Posted - 02/04/2013 :  11:48:03  Show Profile  Reply with Quote
Thanks James K that is brilliant thank you.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3588 Posts

Posted - 02/04/2013 :  15:55:28  Show Profile  Reply with Quote
You are very welcome - glad to be of help.
Go to Top of Page

AlmalynC88
Starting Member

Philippines
1 Posts

Posted - 02/05/2013 :  23:44:55  Show Profile  Reply with Quote
IIF is a shorthand way for writing a CASE expression. It evaluates the Boolean expression passed as the first argument, and then returns either of the other two arguments based on the result of the evaluation. That is, the true_value is returned if the Boolean expression is true, and the false_value is returned if the Boolean expression is false or unknown. true_value and false_value can be of any type. The same rules that apply to the CASE expression for Boolean expressions, null handling, and return types also apply to IIF. For more information, see CASE (Transact-SQL).
The fact that IIF is translated into CASE also has an impact on other aspects of the behavior of this function. Since CASE expressions can be nested only up to the level of 10, IIF statements can also be nested only up to the maximum level of 10. Also, IIF is remoted to other servers as a semantically equivalent CASE expression, with all the behaviors of a remoted CASE expression.

>>simple IIF sample
DECLARE @a int = 45, @b int = 40;
SELECT IIF ( @a > @b, 'TRUE', 'FALSE' ) AS Result;
Here is the result set.
Result
--------
TRUE

(1 row(s) affected)
B. IIF with NULL constants
SELECT IIF ( 45 > 30, NULL, NULL ) AS Result;
The result of this statement is an error.
C. IIF with NULL parameters
DECLARE @P INT = NULL, @S INT = NULL;
SELECT IIF ( 45 > 30, @p, @s ) AS Result;
Here is the result set.
Result
--------
NULL

(1 row(s) affected)

Almalyn
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000