Can MySQL replace multiple characters?

前端 未结 6 2068
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 05:42

I\'m trying to replace a bunch of characters in a MySQL field. I know the REPLACE function but that only replaces one string at a time. I can\'t see any appropriate function

6条回答
  •  攒了一身酷
    2020-11-27 06:26

    CREATE FUNCTION IF NOT EXISTS num_as_word (name TEXT) RETURNS TEXT RETURN
    (
        SELECT 
            REPLACE(
                REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE(
                                REPLACE(                            
                                    REPLACE(
                                        REPLACE(                            
                                            REPLACE(IFNULL(name, ''), 
                                                '1', 'one'), 
                                                '2', 'two'), 
                                                '3', 'three'), 
                                                '4', 'four'), 
                                                '5', 'five'), 
                                                '6', 'six'), 
                                                '7', 'seven'),                                                          
                                                '8', 'eight'),
                                                '9', 'nine')
    );
    

提交回复
热议问题