Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to convert data to sql proper case?

Asked by Gloria Holl May 22, 2024 12.6K views 3 answers
Share

About this question

SQL Server contains system functions for viewing / updating string data to both uppercase and lowercase but not proper case. There are multiple reasons for desiring this operation take place in SQL Server rather than at the application layer. In my case, we were performing some data cleanup during a consolidation of our global HR data from multiple sources.

If you search the internet you will find multiple solutions to this task but many seem to have restrictive caveats or do not allow for exceptions to be defined in the function.

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jan 21, 2025

If you’re trying to convert data to SQL proper case (where the first letter of each word is capitalized, and the rest are lowercase), SQL doesn’t have a direct built-in function, but you can achieve this in several ways. Here’s how:

1. Use INITCAP() in Oracle:

  • Oracle provides a built-in INITCAP() function to convert text to proper case.

  SELECT INITCAP(column_name) AS proper_caseFROM table_name;

  • Example: "john doe" becomes "John Doe".

2. Write a User-Defined Function (UDF) in SQL Server:

  • SQL Server doesn’t have a built-in function like Oracle, but you can create a custom function.

  CREATE FUNCTION dbo.ToProperCase(@input NVARCHAR(MAX))RETURNS NVARCHAR(MAX)ASBEGIN    RETURN (SELECT STRING_AGG(UPPER(LEFT(value, 1)) + LOWER(SUBSTRING(value, 2, LEN(value))), ' ')            FROM STRING_SPLIT(@input, ' '));END;

  • Use it like this:

  SELECT dbo.ToProperCase('john doe') AS proper_case;

3. Use String Manipulation in MySQL:

  • MySQL doesn’t have a direct function either, but you can use string functions like CONCAT(), UPPER(), and LOWER().

  SELECT CONCAT(UPPER(LEFT(column_name, 1)), LOWER(SUBSTRING(column_name, 2))) AS proper_caseFROM table_name;

4. Handle Multi-Word Strings in Complex Scenarios:

  • For multi-word strings in databases without INITCAP(), you may need procedural languages like PL/pgSQL (PostgreSQL) or Python (for external processing).

5. Best Practices:

  • Be cautious with edge cases like all-uppercase or mixed-case strings.
  • Proper case might not suit names with special rules (e.g., McDonald, O'Connor).

Using these methods, you can transform your data to proper case based on the SQL database you're working with. Let me know if you’d like a deeper dive into any of these!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 25, 2024

Converting data to proper case in SQL involves capitalizing the first letter of each word in a string while converting the rest to lowercase. Here's how you can achieve this in different SQL database systems:


MySQL:

MySQL doesn't have a built-in function for proper case conversion. However, you can use a combination of LOWER(), UPPER(), CONCAT(), and SUBSTRING() functions to accomplish this.

PostgreSQL:

PostgreSQL provides the INITCAP() function, which converts the first letter of each word in a string to uppercase and the rest to lowercase.

  SQL Server (Transact-SQL):

SQL Server doesn't offer a native proper case function, but you can create a user-defined function (UDF) for this purpose.

Oracle:

Oracle has the INITCAP() function, similar to PostgreSQL, for converting strings to proper case.

These methods allow you to convert data to proper case in SQL, catering to different database systems' syntax and functionalities.


Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.