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

CREATE  FUNCTION [dbo].[CF_Cbte_xCampo](@pId_Trans INT, @pId_Campo INT) 
RETURNS CHAR(5000)
AS
BEGIN
DECLARE @Retorno VARCHAR(5000)


SELECT @Retorno = CASE UPPER(LTRIM(RTRIM(TipoDato)))
					WHEN 'STRING'	THEN Valor_String 
					WHEN 'INTEGER'	THEN CAST(Valor_Integer AS VARCHAR(50))
					WHEN 'DECIMAL'	THEN CAST(Valor_Decimal AS VARCHAR(100))
					WHEN 'MEMO'		THEN Valor_Memo 
					WHEN 'DATE'		THEN CONVERT(VARCHAR(8),Valor_DT ,112)
					WHEN 'TIME'		THEN CONVERT(VARCHAR(8),Valor_DT ,114)
					END
					FROM Cbtes_XCampos CX, xCampos X
					WHERE CX.Id_Campo = @pId_Campo
					AND CX.Id_Trans = @pId_Trans
					AND X.Id_Campo = CX.Id_Campo

RETURN ISNULL(@Retorno, '')
END
