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.
Author |
Topic |
forrestgump
Starting Member
19 Posts |
Posted - 2013-02-04 : 09:52:25
|
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
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-04 : 10:01:44
|
An exact translation of your expression in a T-SQL SELECT would beCASE 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 |
|
|
forrestgump
Starting Member
19 Posts |
Posted - 2013-02-04 : 11:48:03
|
Thanks James K that is brilliant thank you. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-04 : 15:55:28
|
You are very welcome - glad to be of help. |
|
|
AlmalynC88
Starting Member
1 Post |
Posted - 2013-02-05 : 23:44:55
|
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 sampleDECLARE @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 constantsSELECT IIF ( 45 > 30, NULL, NULL ) AS Result;The result of this statement is an error.C. IIF with NULL parametersDECLARE @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 |
|
|
|
|
|
|
|