mysql: How can I remove character at start or end of field

后端 未结 4 1533
陌清茗
陌清茗 2021-01-03 21:18

I have field that contains a comma-separated list. Some old records have a comma at the beginning or end of the field. I need to remove these extra commas.

Example:<

相关标签:
4条回答
  • 2021-01-03 21:28

    The question was how to remove the leading and trailing characters I will show you an example with an update query.

    UPDATE your_table_name
    SET your_record_name = TRIM(BOTH ',' FROM your_record_name)
    
    0 讨论(0)
  • 2021-01-03 21:34

    @codingbiz, Thank you for the website link:

    Quick examples:

    SELECT TRIM(BOTH ',' FROM fieldname) from tablename
    
    SELECT TRIM(LEADING ',' FROM fieldname) from tablename
    
    SELECT TRIM(TRAILING ',' FROM fieldname) from tablename
    

    Thanks!

    0 讨论(0)
  • 2021-01-03 21:35

    You are looking for the TRIM function.

    0 讨论(0)
  • 2021-01-03 21:52

    Check this website

    SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
    

    which in your case would be ',' instead of 'xyz'

    0 讨论(0)
提交回复
热议问题