SQL Server IIFSTR Function
Updated 2023-11-10 16:27:27.003000
Description
Use the scalar function IIFSTR to return one of two values, depending on the evaluation of an expression.
Syntax
SELECT [westclintech].[wct].[IIFSTR] (
<@Condition, bit,>
,<@TrueValue, nvarchar(max),>
,<@FalseValue, nvarchar(max),>)
Arguments
@Condition
the expression being evaluated. The @Condition argument can be an expression of types that are implicitly convertible to bit.
@TrueValue
the value returned if @Condition is true. The @Truevalue argument can be of data types that are implicitly convertible to nvarchar or ntext.
@FalseValue
the value returned if @Condition is false. The @Falsevalue argument can be of data types that are implicitly convertible to nvarchar or ntext.
Return Type
nvarchar(max)
Examples
CREATE TABLE #i
(
[recno] [float] NOT NULL,
[numerator] [float] NOT NULL,
[denominator] [float] NOT NULL
);
INSERT INTO #i
VALUES
(1, 3, -1);
INSERT INTO #i
VALUES
(1, 3, 0);
INSERT INTO #i
VALUES
(1, 3, 1);
SELECT recno,
numerator,
denominator,
Numerator / wct.IIFSTR(denominator, denominator, .01)
from #i;
This produces the following result.
| recno | numerator | denominator | |
|---|---|---|---|
| 1 | 3 | -1 | -3 |
| 1 | 3 | 0 | 300 |
| 1 | 3 | 1 | 3 |