Techblog Index

Quick Tip: Capitalize field in MySql

This one was quite interesting. I’ve wrote my PHP function to capitalize the fileld at insert with ucwords() but I already had some on the base that I need to convert without re-importing them.

Googled for the answer and found two good pieces of code:

If you want it simple, like ucfirst(), getting only the very first letter of the first word, you may have:

UPDATE `table` SET target_field =
        CONCAT(
                UCASE(SUBSTRING(`source_field`, 1, 1)),
                LOWER(SUBSTRING(`source_field`, 2))
        );

Just replace ‘table’ with the table’s name and ‘target_field’ with your… target field?

If you want the job done in all terms, Joe Zack’s MySQL Capitalize Function may fit you as well as it fit me.

Who’s got a better idea?

 
 

Leave a Reply