DROP FUNCTION IF EXISTS fnGetRendszerbeallitasEnumBool;  
GO

CREATE FUNCTION fnGetRendszerbeallitasEnumBool (@beallitasTipus int, @intezmenyId int, @tanevId int)
  RETURNS int
BEGIN  
  DECLARE @value INT
  SELECT @value =
    CASE 
      WHEN C_ERTEKTIPUS = 1698 
      THEN(
        SELECT Value FROM
        OPENJSON( JSON_QUERY(C_ERTEK,'$.Options'))
        WITH (   
          Selected   nvarchar(10)   '$.Selected',  
          Value     int       '$.Value'
        )
        WHERE Selected = 'true')
      WHEN C_ERTEKTIPUS = 1699 
        THEN IIF(JSON_VALUE(C_ERTEK, '$.Value') = 'true', 1, 0)
      ELSE NULL
    END
  FROM T_RENDSZERBEALLITAS_OSSZES
  WHERE C_BEALLITASTIPUS = @beallitasTipus
    AND C_INTEZMENYID = @intezmenyId
    AND C_TANEVID = @tanevId
	AND TOROLT = 'F'
  RETURN @value
END;
GO
