2

I need a script to run that will update 'given_name' column and change all first letters to capital..

I currently have this code.. and nothing happens..

DROP PROCEDURE IF EXISTS CAPITALIZE;
DELIMITER $$

CREATE PROCEDURE CAPITALIZE()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
SELECT COUNT(*) INTO n FROM users;
-- SET n = (SELECT COUNT(*) FROM users);
SET i=0;
WHILE (i<=n) DO 
  UPDATE users SET given_name = CONCAT( UPPER(SUBSTR(given_name,1,1)), SUBSTR(given_name,2)) WHERE user_id = n;
  UPDATE users SET last_name = CONCAT( UPPER(SUBSTR(last_name,1,1)), SUBSTR(last_name,2)) WHERE user_id = n;

  SET i = (i + 1);
END WHILE;
END $$
DELIMITER ;
EdSniper
  • 271
  • 4
  • 21

1 Answers1

4

As @Drew mentioned, you might be better off just doing a blanket UPDATE statement for the entire users table:

UPDATE users
SET given_name = CONCAT( UPPER(SUBSTR(given_name,1,1)), LOWER(SUBSTR(given_name,2))),
    last_name = CONCAT( UPPER(SUBSTR(last_name,1,1)), LOWER(SUBSTR(last_name,2)))

To turn off your safe update mode, which is preventing the above query from working, type this:

SET SQL_SAFE_UPDATES = 0;

Here is a working fiddle which demonstrates the above query:

SQLFiddle

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360