Analysis and Implementation of Proper Case Conversion User-Defined Functions in SQL Server

Nov 19, 2025 · Programming · 11 views · 7.8

Keywords: SQL Server | Proper Case | User-Defined Function | Case Conversion | String Processing

Abstract: This article provides an in-depth exploration of converting all-uppercase text to Proper Case (title case) in SQL Server. By analyzing multiple user-defined function solutions, it focuses on efficient algorithms based on character traversal and state machines, detailing function design principles, code implementation, and practical application scenarios. The article also discusses differences among various approaches in handling special characters, multilingual support, and performance optimization, offering valuable technical references for database developers.

Technical Background of Proper Case Conversion

In database application development, there is often a need to convert all-uppercase text data to Proper Case (title case format). While SQL Server provides UPPER() and LOWER() functions for case conversion, it lacks built-in Proper Case functionality. This conversion holds significant value in scenarios such as data cleansing, report generation, and user interface presentation.

Core Algorithm Principle Analysis

The core algorithm for Proper Case conversion is based on character traversal and state machine design. The basic approach involves: traversing each character of the input string and determining the case conversion state based on the type of the previous character. When non-alphabetic characters (such as spaces, punctuation, etc.) are encountered, the next alphabetic character is marked for uppercase conversion, while remaining alphabetic characters are converted to lowercase.

Detailed Implementation of Main Function

Based on the best answer from the Q&A data, we have implemented an efficient Proper Case conversion function:

CREATE FUNCTION ProperCase(@Text AS VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
  DECLARE @Reset BIT;
  DECLARE @Ret VARCHAR(8000);
  DECLARE @i INT;
  DECLARE @c CHAR(1);

  IF @Text IS NULL
    RETURN NULL;

  SELECT @Reset = 1, @i = 1, @Ret = '';

  WHILE (@i <= LEN(@Text))
    SELECT @c = SUBSTRING(@Text, @i, 1),
      @Ret = @Ret + CASE WHEN @Reset = 1 THEN UPPER(@c) ELSE LOWER(@c) END,
      @Reset = CASE WHEN @c LIKE '[a-zA-Z]' THEN 0 ELSE 1 END,
      @i = @i + 1
  RETURN @Ret
END

Function Mechanism Explanation

This function maintains the case conversion state through the @Reset variable: when @Reset is 1, it indicates that the next alphabetic character should be converted to uppercase; when @Reset is 0, it indicates that the function is within a word and alphabetic characters should be converted to lowercase. The function traverses each character and uses a CASE statement to determine the case conversion based on the @Reset state.

The state transition logic is based on character type determination: if the current character is alphabetic (determined by LIKE '[a-zA-Z]' pattern matching), @Reset is set to 0, indicating entry into the interior of a word; if the current character is non-alphabetic, @Reset is set to 1, indicating a word boundary has been encountered and the next alphabetic character needs capitalization.

Performance Optimization Considerations

The function design carefully considers performance factors: it uses a simple WHILE loop for character traversal, avoiding complex string operations; employs a bit variable @Reset for state management, reducing conditional judgment overhead; and supports VARCHAR(8000) length, capable of handling most practical application scenarios.

Practical Application Examples

Assuming a table containing all-uppercase data, the following SQL statement can be used for batch conversion:

UPDATE YourTable 
SET ColumnName = dbo.ProperCase(ColumnName)
WHERE ColumnName IS NOT NULL

Conversion effect example: input "ALL UPPER CASE TEXT" will be converted to "All Upper Case Text", while input "SOME lower CASE" will maintain the format as "Some Lower Case".

Comparative Analysis with Other Solutions

Some alternative implementations mentioned in the reference articles have limitations when handling special scenarios. For instance, certain solutions cannot properly handle words containing apostrophes (like O'Malley), or have insufficient support for non-English characters. In contrast, the main solution presented in this article achieves a good balance between simplicity and generality.

Extension Function Suggestions

For applications requiring more complex scenario handling, consider the following extensions: support for Unicode character sets by using NVARCHAR data types and NCHAR functions; addition of custom delimiter configuration, allowing users to define which characters trigger uppercase conversion; implementation of exception word handling for specific words (such as "iPhone", "eBay", etc.) requiring special treatment.

Conclusion

Implementing Proper Case conversion through user-defined functions is a common requirement in SQL Server data processing. The state machine-based algorithm solution introduced in this article offers advantages of concise code, good performance, and easy comprehension, making it suitable for most practical application scenarios. Developers can choose appropriate implementation solutions based on specific requirements or extend functionality based on this foundation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.