USE <Database_Name>
GO
IF OBJECT_ID(N'udf__SubsTextParam') IS NOT NULL
DROP FUNCTION udf__SubsTextParam
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- *****************************************************************************
-- Name : udf__SubsTextParam
-- Author : Chandra Gunawan
-- Date : 14-Sep-12
-- Description : Return the substituted text parameter
-- Notes : Format parameter in text is {}
-- is sequnce number of parameter list, starting with 0
-- e.g.: @pText => Customer {0} activated on {1}
-- @pParamList => 12000001,1-Jan-2012
-- @pDelimiter => ,
-- RETURN => Customer 12000001 activated on 1-Jan-2012
--
-- Usage Sample :
-- -------------------------------------------------
-- SELECT dbo.udf__SubsTextParam('Customer {0} activated on {1}',
-- '12000001,1-Jan-2012',
-- ',')
-- -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date ID Description
-- -----------------------------------------------------------------------------
-- 14-Sep-12 chg Initial Revision
-- *****************************************************************************
CREATE FUNCTION [dbo].[udf__SubsTextParam](
@pText varchar(1000), -- Input Text
@pParamList varchar(500), -- Parameter List
@pDelimiter varchar(5) = ',' -- Delimiter
)
RETURNS varchar(2000)
AS
BEGIN
DECLARE @i int,
@j int,
@cParam varchar(500)
-- Process substitution
SET @j = -1
WHILE @pParamList <> ''
BEGIN
SELECT @j = @j + 1,
@i = CHARINDEX(@pDelimiter, @pParamList)
IF @i = 0
SELECT @cParam = @pParamList,
@pParamList = ''
ELSE
SELECT @cParam = SUBSTRING(@pParamList, 1, @i - 1),
@pParamList = SUBSTRING(@pParamList, @i + LEN(@pDelimiter), LEN(@pParamList))
SET @pText = REPLACE(@pText, '{' + CONVERT(varchar, @j) + '}', @cParam)
END
RETURN @pText
END
GO
GRANT EXECUTE ON dbo.udf__SubsTextParam TO public
GO
No comments:
Post a Comment