CREATE FUNCTION dbo.Proper_Case (
@string varchar(2000)
)
RETURNS varchar(2000)
AS
--Sample use: SELECT dbo.Proper_Case('aBC mike;a alkj/as 12k 43')
BEGIN
DECLARE @previousByte int
DECLARE @byte int
SET @string = LOWER(@string)
IF LEFT(@string, 1) LIKE '[a-z]'
SET @string = STUFF(@string, 1, 1, UPPER(LEFT(@string, 1)))
SET @previousByte = 2
WHILE 1 = 1
BEGIN
SET @byte = PATINDEX('%[^a-z0-9][a-z]%', SUBSTRING(@string, @previousByte, 2000))
--PRINT 'Byte = ' + CAST(@byte AS varchar(10))
IF @byte = 0
BREAK
SET @string = STUFF(@string, @previousByte + @byte, 1,
UPPER(SUBSTRING(@string, @previousByte + @byte, 1)))
SET @previousByte = @previousByte + @byte + 1
--PRINT 'PrevByte = ' + CAST(@previousByte AS varchar(10))
END --WHIILE
RETURN @string
END --FUNCTION
GO
For the following data
CREATE TABLE Table1
([Column1] varchar(60))
;
INSERT INTO Table1
([Column1])
VALUES
('patrick o''malley'),
('wolfgang von ribben tropp'),
('buckley & nunn'),
('archibald smyth-percival')
;
This query produced the following result:
SELECT
dbo.Proper_Case(Column1)
FROM Table1
;
Patrick O'Malley
Wolfgang Von Ribben Tropp
Buckley & Nunn
Archibald Smyth-Percival
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
ASKER