IF  EXISTS (SELECT 1 FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[F_PADRONIZA_AGRUPAMENTO]') AND TYPE IN (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [F_PADRONIZA_AGRUPAMENTO]
GO 

CREATE FUNCTION [dbo].[F_PADRONIZA_AGRUPAMENTO] (@Texto VARCHAR(MAX)) 
RETURNS VARCHAR(MAX) AS
BEGIN


	 DECLARE @String varchar(MAX) = @Texto
	 DECLARE @StringAux varchar(MAX) = ''

	 SET @StringAux = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(@String, ' =', '='), '= ', '='), '  ', ' ')))

		WHILE (@StringAux <> @String)
		BEGIN

			SET @String = @StringAux

			SET @StringAux = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(@String, ' =', '='), '= ', '='), '  ', ' ')))
		
		END

	RETURN @StringAux
END
GO


