Monday, November 4, 2013

SQL - Remove Line Breaks From Text

This function will attempt to remove line breaks from text in SQL.
USE MIGRATION_DB
GO
IF OBJECT_ID (N'dbo.RemoveLineBreak', N'FN') IS NOT NULL
DROP FUNCTION dbo.RemoveLineBreak;
GO
CREATE FUNCTION dbo.RemoveLineBreak
(@TEXT VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN

DECLARE @fixedTEXT VARCHAR(MAX) = @TEXT

SET @fixedTEXT = REPLACE(REPLACE(REPLACE(REPLACE(@TEXT, CHAR(10), ''), CHAR(13), ''), '\r', ' '), '\n', ' ')

RETURN @fixedTEXT
END
GO

No comments: