Click here to Skip to main content
15,890,670 members
Articles / Programming Languages / SQL
Alternative
Tip/Trick

SQL Convert Simple Names to Proper Case

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Nov 2017CPOL1 min read 7.7K   2   1
This is an alternative for "SQL Convert Sentence to Proper Case"

Introduction

As with the original tip, this code is limited to affecting the casing of simple names -- by which I mean that you know that each and every "word" in the value should begin with one capital letter.

Background

This is submitted only as an Transact-SQL alternative. Such string manipulation should not be done in SQL, but here's how it can be done with a Recursive Common Table Expression.

Using the Code

You'll need the table you want to update. I'm using a temporary table just to test.

SQL
DROP TABLE #temp
;
SELECT *
INTO #temp
FROM
(
  SELECT 'IOWA' [foo]
UNION ALL
  SELECT 'iowa' [foo]
UNION ALL
  SELECT 'NORTH CAROLINA'
UNION ALL
  SELECT 'north carolina'
UNION ALL
  SELECT 'NEW SOUTH WALES'
UNION ALL
  SELECT 'new south wales'
) T
; 
SELECT * 
FROM #temp

First, I'm using a regular Common Table Expression just to get a list of all the distinct values after they have been made uppercase. The key is to find a pair of consecutive uppercase letters and then a SPACE following the "word". Be sure to append a SPACE to the input value to ensure that you always have a SPACE to find. I don't like having to cast to VARCHAR(MAX), so maybe someone can find a way to avoid that.

SQL
;
WITH [cte0] AS
(
  SELECT DISTINCT CAST ( UPPER ( [foo] ) + ' ' AS VARCHAR(MAX) ) [foo]
  , CAST ( PATINDEX ( '%[A-Z][A-Z]%' COLLATE Latin1_General_BIN , _
  [foo] COLLATE Latin1_General_BIN ) AS BIGINT ) [bar]
  FROM #temp
)

And here's the Recursive Common Table Expression. It repeatedly replaces the part of a "word" after the leading uppercase letter with the lowercase version until there are no more pairs of consecutive uppercase letters.

SQL
, [cte1] AS
(
  SELECT [foo]
  , [bar]
  , CHARINDEX ( ' ' , [foo] , [bar] ) [baz]
  FROM [cte0]
  WHERE [bar] > 0
UNION ALL
  SELECT [foo]
  , [bar]
  , CHARINDEX ( ' ' , [foo] , [bar] ) [baz]
  FROM
  (
    SELECT [foo]
    , PATINDEX ( '%[A-Z][A-Z]%' COLLATE Latin1_General_BIN , [foo] COLLATE Latin1_General_BIN ) [bar]
    FROM
    (
      SELECT SUBSTRING ( [foo] , 1 , [bar] ) + LOWER ( SUBSTRING ( [foo] , [bar] + 1 , _
      [baz] - [bar] - 1 ) ) + SUBSTRING ( [foo] , [baz] , DATALENGTH ( [foo] ) ) [foo]
      FROM [cte1]
      WHERE [bar] > 0
    ) T
  ) U
)

The result can then be used to UPDATE the table.

SQL
UPDATE #temp
SET [foo] = RTRIM ( B.[foo] )
FROM #temp A
INNER JOIN [cte1] B
ON A.[foo] = RTRIM ( B.[foo] )
AND B.[bar] = 0
; 
SELECT * 
FROM #temp

History

  • 2017-11-19 First submission

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB, acknowledged pedant and contrarian

---------------

"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]

"Typing is no substitute for thinking." -- R.W. Hamming

"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon

"linq'ish" sounds like "inept" in German -- Andreas Gieriet

"Things would be different if I ran the zoo." -- Dr. Seuss

"Wrong is evil, and it must be defeated." –- Jeff Ello

"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw

“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

"Omit needless local variables." -- Strunk... had he taught programming

Comments and Discussions

 
QuestionSTRING_SPLIT and STRING_AGG? Pin
Chris Maunder31-Mar-21 7:05
cofounderChris Maunder31-Mar-21 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.