Years ago I had the pleasure of meeting Ben Forta and listen to his thoughts on web development. One thing that stuck with me was his statement that:
“Dynamic programming languages like PHP and Coldfusion are great, but developers need to leverage the database more. Allow the database to do it’s job, the heavy lifting”
With that being said, I was recently working with a database that contained users first names. The data was not snantized at entry, and contained a mix of lower cased (ex. josh) and upper cased (ex. Josh) first characters in the firstname column. I needed to always present the users first name in a capitalized format.
The first solution that most developers would reach for is dynamically parsing the users first name, then upper-casing the first character. This works, but in my opinion isn’t the most elegant solution.
With a simple SQL statement, like the one below, we can return the users first name already in a capitalized state.
SELECT (UPPER(LEFT(firstname,1))+SUBSTRING(firstname,2,LEN(firstname))) AS firstname FROM users