postgresql

IIF in postgres


I am attempting to convert an MS-Access query to a postgres statement so I can use it in SSRS. Seems to work great except for the IIF statement.

SELECT labor_sort_1.ncm_id
,IIf(labor_sort_1.sortby_employeeid = 3721
 , ((labor_sort_1.MaxUpdatedAt - labor_sort_1.MinNCMScanTime) * 24 * 29 * labor_sort_1.number_of_ops)
 , IIf(labor_sort_1.sortby_employeeid = 3722
  , ((labor_sort_1.MaxUpdatedAt - labor_sort_1.MinNCMScanTime) * 24 * 24 * labor_sort_1.number_of_ops)
  , IIf(labor_sort_1.sortby_employeeid = 3755, ((labor_sort_1.MaxUpdatedAt - labor_sort_1.MinNCMScanTime) * 24 * 24 * labor_sort_1.number_of_ops)
  , ((labor_sort_1.MaxUpdatedAt - labor_sort_1.MinNCMScanTime) * 24 * 17 * labor_sort_1.number_of_ops)))) AS labor_cost
FROM ...

it returns the following message function iif(boolean, interval, interval) does not exist How would I solve this problem?


Solution

  • I know this has been sitting around for a while but another option is to create a user defined function. If you happen to stumble upon this in your internet searches, this may be a solution for you.

        CREATE FUNCTION IIF(
            condition boolean, true_result TEXT, false_result TEXT
        ) RETURNS TEXT LANGUAGE plpgsql AS $$
        BEGIN
         IF condition THEN
            RETURN true_result;
         ELSE
            RETURN false_result;
         END IF;
        END
        $$;
    
        SELECT IIF(2=1,'dan the man','false foobar');
    

    Should text not tickle your fancy then try function overloading