Home >Database >Mysql Tutorial >How to Convert All Uppercase Strings to Proper Case in SQL Server?

How to Convert All Uppercase Strings to Proper Case in SQL Server?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-10 08:47:41775browse

How to Convert All Uppercase Strings to Proper Case in SQL Server?

Convert all-caps string to first-letter case in SQL Server

In SQL Server, you may encounter a situation where the imported data uses uppercase characters and needs to be converted to first letter capitalization. This improves the readability and consistency of database records. Here is the solution on how to achieve this conversion:

Create a capitalization conversion function

The following T-SQL function ToProperCase() effectively converts all-capital words to first-letter case while leaving lowercase words unchanged:

<code class="language-sql">CREATE FUNCTION ToProperCase(@string VARCHAR(255)) RETURNS VARCHAR(255)
AS
BEGIN
  -- 函数逻辑在此处...
END;</code>

Function implementation

This function iterates over the input string character by character, identifying uppercase letters and setting flags to indicate the start of a new word. It then concatenates the current character to the output string, converting the uppercase character to lowercase if it is not the first letter of the word.

Example usage

To use this function, you call it as follows:

<code class="language-sql">SELECT dbo.ToProperCase('ALL UPPER CASE AND SOME lower ÄÄ ÖÖ ÜÜ ÉÉ ØØ ĈĈ ÆÆ') AS ProperCaseString;</code>

Results

The query will return the following results:

<code>ProperCaseString
-----------------------------------------------------------------
All Upper Case And Some lower Ää Öö Üü Éé Øø Ĉĉ Ææ</code>

Customized

You can customize the function to support different space characters or character encodings by modifying the @w variable and using NCHAR and NVARCHAR respectively for Unicode support.

The above is the detailed content of How to Convert All Uppercase Strings to Proper Case in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn