CREATE OR REPLACE FUNCTION dbo.fgetsystemparameter(IN par_category varchar, IN par_subcategory varchar,
    IN par_name varchar, IN par_value int DEFAULT 0, IN par_default varchar DEFAULT NULL)
RETURNS varchar
AS
$BODY$
BEGIN
    RETURN (SELECT
        COALESCE(CASE
            WHEN par_Value = 0 THEN sp.value
            WHEN par_Value = 1 THEN sp.value1
            WHEN par_Value = 2 THEN sp.value2
        END, par_Default)
        FROM dbo.systemparameter AS sp
        WHERE LOWER(sp.category) = LOWER(par_Category) AND LOWER(sp.subcategory) = LOWER(par_SubCategory) AND LOWER(sp.name) = LOWER(par_Name));
END;
$BODY$
LANGUAGE  plpgsql VOLATILE;