Mysql:Trim all fields in database

前端 未结 7 1925
耶瑟儿~
耶瑟儿~ 2021-02-12 08:37
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:52

    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.

提交回复
热议问题