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

CREATE FUNCTION [dbo].[CF_SCA_Cliente_ApeNom](@pId_Cliente INT)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @Ape_Nom		VARCHAR(100)
DECLARE @Apellido		VARCHAR(100)
DECLARE @Nombre			VARCHAR(100)
DECLARE @Razon_Social	VARCHAR(100)

SELECT  @Apellido		= ISNULL(Apellido, ''),
		@Nombre			= ISNULL(Nombre, ''),
		@Razon_Social	= ISNULL(Razon_Social, '')
FROM Clientes
WHERE 
Id_Cliente = @pId_Cliente

SET @Apellido = REPLACE(@Apellido, ',', '')
SET @Nombre = REPLACE(@Nombre, ',', '')
SET @Razon_Social = REPLACE(@Razon_Social, ',', '')

IF(@Apellido <> '')
BEGIN
	SET @Ape_Nom = @Apellido + ' ' + @Nombre
END
ELSE
BEGIN
	SET @Ape_Nom = @Razon_Social
END

RETURN ISNULL(@Ape_Nom, '')

END