IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[CF_Json_ParsearVariable]') 
                   and type = 'FN')
BEGIN
    DROP FUNCTION [dbo].[CF_Json_ParsearVariable] 
END

GO

CREATE FUNCTION [dbo].[CF_Json_ParsearVariable](@pVariable VARCHAR(4000), @pValor VARCHAR(4000), @pTipoDato VARCHAR(20))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Json		VARCHAR(8000)
DECLARE @Fecha		DATETIME
DECLARE @FechaVC	VARCHAR(30)


IF(@pTipoDato = 'DATETIME')
BEGIN
	SET @Fecha = CONVERT(DATETIME, @pValor)
	SET @FechaVC = REPLACE(CONVERT(VARCHAR, @Fecha, 111), '/', '-') + 'T' + CONVERT(VARCHAR, @Fecha, 8)
	
	SET @Json = '"' + LTRIM(RTRIM(@pVariable)) + '"' + ':' + '"' + @FechaVC + '"'
	
	RETURN @Json
END


IF(@pTipoDato = 'DATE')
BEGIN
	SET @Fecha = CONVERT(DATETIME, @pValor)
	SET @FechaVC = REPLACE(CONVERT(VARCHAR, @Fecha, 111), '/', '-') + 'T00:00:00'
	
	SET @Json = '"' + LTRIM(RTRIM(@pVariable)) + '"' + ':' + '"' + @FechaVC + '"'
	
	RETURN @Json
END

ELSE
BEGIN
	SET @Json = '"' + LTRIM(RTRIM(@pVariable)) + '"' + ':' + '"' + @pValor + '"'
	
	RETURN @Json
END

RETURN ISNULL(@Json, '"":""')
END
