How do I change all empty strings to NULL in a table?

前端 未结 7 1507
悲哀的现实
悲哀的现实 2020-12-08 20:25

I have a legacy table with about 100 columns (90% nullable). In those 90 columns I want to remove all empty strings and set them to null. I know I can:

updat         


        
7条回答
  •  独厮守ぢ
    2020-12-08 20:49

    You could write a simple function and pass your columns to it:

    Usage:

    SELECT
      fn_nullify_if_empty(PotentiallyEmptyString)
    FROM
      table_name
    ;
    

    Implementation:

    DELIMITER $$
    CREATE FUNCTION fn_nullify_if_empty(in_string VARCHAR(255))
      RETURNS VARCHAR(255)
      BEGIN
        IF in_string = ''
          THEN RETURN NULL;
          ELSE RETURN in_string;
        END IF;
      END $$
    DELIMITER ;
    

提交回复
热议问题