Search code examples
phpmysqlsqltitle-case

Using the Title case in an SQL statement


I have tried various Title Case solutions from here and they give me an 0 in the column. I have to change the LEN to LENGTH to get it to run. If I strip the statement down to its 2 parts it works.

 UPDATE names 
    SET
    surname = UPPER(LEFT(surname, 1)) +
    LOWER(RIGHT(surname, LENGTH(surname) - 1));

Or

update  names
set     surname = upper(substring(surname,1,1)) + 
        lower(substring(surname, 2, length(surname)-1))
where   length(surname) > 0 ;

Solution

  • If you use MySql you must use function CONCAT, to concatenate two strings.

    In this way:

    update  names
    set     surname = concat(upper(substring(surname,1,1)),
            lower(substring(surname, 2, length(surname)-1)))
    where   length(surname) > 0 ;
    

    It's good use WHERE condition so you prevent possible nullable field

    N.B. Your result is 0 because DBMS tries to sum two number (implicit conversion by your string, so you'll get 0 instead your result).

    Show Fiddle