Skapa en tabell (jag använder TITLE_CASE_EXCEPTION som mitt exempel) med en kolumn EXCEPTIONDå är det datadrivet därifrån.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[GUI].[fn_TITLE_CASE]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [GUI].[fn_TITLE_CASE]
GO
CREATE FUNCTION [GUI].[fn_TITLE_CASE]
(
@STRING VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
SET QUOTED_IDENTIFIER OFF
DECLARE @RESET BIT
DECLARE @_OUT_STRING VARCHAR(MAX)
DECLARE @I INT
DECLARE @C CHAR(1)
DECLARE @CASE_LEN INT = 0
DECLARE @CASE_EXCEPTIONS VARCHAR(MAX) = ''
DECLARE @CASE_VALUE VARCHAR(MAX) = ''
IF @STRING IS NULL
RETURN NULL
IF @STRING = ''
RETURN @STRING
SELECT @STRING = LOWER(RTRIM(@STRING)), @RESET = 1, @I = 1, @_OUT_STRING = ''
WHILE (@I <= LEN(@STRING))
SELECT
@C = SUBSTRING(@STRING, @I, 1),
@_OUT_STRING = @_OUT_STRING + CASE WHEN @RESET = 1 THEN UPPER(@C) ELSE @C END,
@RESET = CASE WHEN @C LIKE '[a-zA-Z'']' THEN 0 ELSE 1 END,
@I = @I + 1
SELECT @I = 0, @_OUT_STRING = @_OUT_STRING + ' '
SELECT @CASE_EXCEPTIONS = @CASE_EXCEPTIONS + RTRIM(EXCEPTION) + ',' FROM [LOOKUP].TITLE_CASE_EXCEPTION
WHILE CHARINDEX(',', @CASE_EXCEPTIONS, @I + 1) > 0
BEGIN
-- get the delimited word
SET @CASE_LEN = CHARINDEX(',', @CASE_EXCEPTIONS, @I + 1) - @I
SET @CASE_VALUE = SUBSTRING(@CASE_EXCEPTIONS, @I, @CASE_LEN)
-- replace it in the original text
SET @_OUT_STRING = REPLACE(@_OUT_STRING, ' ' + @CASE_VALUE + ' ', ' ' + @CASE_VALUE + ' ')
-- get position of next word
SET @I = CHARINDEX(',', @CASE_EXCEPTIONS, @I + @CASE_LEN) + 1
END
RETURN RTRIM(@_OUT_STRING)
END
GO