USE <Database_Name>
GO
IF OBJECT_ID(N'udf__ToSingleSpace') IS NOT NULL
DROP FUNCTION udf__ToSingleSpace
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- *****************************************************************************
-- Name : udf__ToSingleSpace
-- Author : Chandra Gunawan
-- Date : 19-Feb-10
-- Description : Reduce each group of multiple spaces to a single space
-- for a whole table without functions, loops, or other
-- forms of slow RBAR.
-- Parameters :
-- @pInputString vchar(8000) Text to process
--
-- Usage Sample :
-- -------------------------------------------------
-- DECLARE @Demo TABLE(OriginalString varchar(8000))
-- INSERT INTO @Demo (OriginalString)
-- SELECT ' This has multiple unknown spaces in it. ' UNION ALL
-- SELECT 'So does this!' UNION ALL
-- SELECT 'As does this' UNION ALL
-- SELECT 'This, that, and the other thing.' UNION ALL
-- SELECT 'This needs no repair.'
--
-- SELECT OriginalString, dbo.udf__ToSingleSpace(OriginalString) AS CleanString
-- FROM @Demo
-- -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date ID Description
-- -----------------------------------------------------------------------------
-- 19-Feb-10 chg Initial Revision
-- *****************************************************************************
CREATE FUNCTION udf__ToSingleSpace
(
@pInputString varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN
RETURN replace(
replace(
replace(ltrim(rtrim(@pInputString)),
' ', ' ' + char(7)), -- Changes 2 spaces to the OX model
char(7) + ' ', ''), -- Changes the XO model to nothing
char(7), '') -- Changes the remaining X's to nothing
END
GO
GRANT EXECUTE, REFERENCES ON udf__ToSingleSpace TO public
GO
No comments:
Post a Comment