IF OBJECT_ID('fnGetRendszerbeallitasEnumBool') IS NOT NULL BEGIN
  DROP FUNCTION fnGetRendszerbeallitasEnumBool
END
GO

CREATE FUNCTION fnGetRendszerbeallitasEnumBool (@beallitasTipus int, @intezmenyId int, @tanevId int)
  RETURNS int
BEGIN  
  DECLARE @value INT
  SELECT @value =
    CASE 
      WHEN C_ERTEKTIPUS = 1698 
        THEN SUBSTRING(C_ERTEK, CHARINDEX('"Value"',  C_ERTEK, CHARINDEX('"Selected":true', C_ERTEK)) + 9, CHARINDEX('"', C_ERTEK, CHARINDEX('"Value"',  C_ERTEK, CHARINDEX('"Selected":true', C_ERTEK)) + 10) - (CHARINDEX('"Value"',  C_ERTEK, CHARINDEX('"Selected":true', C_ERTEK)) + 9)) 
      WHEN C_ERTEKTIPUS = 1699 
        THEN IIF(C_ERTEK LIKE '%"Value":"true"%' OR C_ERTEK LIKE '%"Value":true%', 1, 0)
      ELSE NULL
    END
  FROM T_RENDSZERBEALLITAS_OSSZES
  WHERE C_BEALLITASTIPUS = @beallitasTipus
    AND C_INTEZMENYID = @intezmenyId
    AND C_TANEVID = @tanevId
  RETURN @value
END;
GO
