Mysql:Trim all fields in database

前端 未结 7 1754
一生所求
一生所求 2021-02-12 08:20
UPDATE mytable SET mycolumn= LTRIM(RTRIM(mycolumn));

works fine on trimming columns removing trailer spaces, but how can i adjust it to trim all column

7条回答
  •  臣服心动
    2021-02-12 08:54

    If there are not too many columns, you could just
    directly UPDATE each by your_column_name, via the TRIM() function:

    UPDATE mytable SET 
    mycolumn1 = TRIM(mycolumn1), 
    mycolumn2 = TRIM(mycolumn2),
    mycolumn3 = TRIM(mycolumn3),
    mycolumn4 = TRIM(mycolumn4)
    

    Otherwise, ZweiStein's answer above for a single table,
    or Izhar Aazmi's answer for an entire database seem the way to go.

    Hiram's answer to another SO Post includes a check to only TRIM VARCHAR fields: excellent feature!

    Or, if using T-SQL, or others which do not support TRIM, use the LTRIM(RTRIM(...)) trick,
    suggested by Jim Rubenstein and Denis de Bernardy above.

提交回复
热议问题