How to implement the opposite of INITCAP() functionality with MySQL?


The INITCAP() method display the first character in every word in uppercase and rest in lowercase.

To implement the opposite functionality, you need to create your own function in MySQL. Here’s the function −

mysql> delimiter //
mysql> create function convertFirstLetterToLowerAndRemainingToCapital(value varchar(250))
   returns varchar(250)
   deterministic
   begin
   declare valueLength int;
   declare l int;
   set valueLength = char_length(value);
   set value = upper(value);
   set l = 0;
   while (l < valueLength ) do
      if (mid(value,l ,1) = ' ' or l = 0) then
         if (l < valueLength ) then
            set value= concat(
               left(value,l),
               lower(mid(value,l+ 1,1)),
               right(value,valueLength - l - 1)
            );
         end if;
      end if;
   set l = l + 1;
   end while;
   return value;
   end;
   //
Query OK, 0 rows affected (0.17 sec)
mysql> delimiter ;

Call the function with the help of select statement −

mysql> select convertFirstLetterToLowerAndRemainingToCapital('Introduction To MySQL');

This will produce the following output −

+-------------------------------------------------------------------------+
| convertFirstLetterToLowerAndRemainingToCapital('Introduction To MySQL') |
+-------------------------------------------------------------------------+
| iNTRODUCTION tO mYSQL                                                   |
+-------------------------------------------------------------------------+
1 row in set, 2 warnings (0.04 sec)

Updated on: 22-Aug-2019

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements