mysql 去掉字段中空格的方法

本文介绍了 mysql 去掉字段中空格的方法

MySQL中去掉字段值中空格的方法有这几个: trim()、ltrim()、rtrim()函数

trim()去除字段首尾空白字符,也可以去除指定字符

去除商品零件号左右空格,以及指定字符,打印去除字符后的长度

select
    p.parts_num as "零件号(包含首尾各3个空格)",
    length(p.parts_num) as "原始长度",
    trim(p.parts_num),
    length(trim(p.parts_num)) as "去除左右空格后长度",
    trim(leading '   7' from p.parts_num),
    length(trim(leading '   7' from p.parts_num)) as "去除左边字符后长度",
    trim(trailing '7   ' from p.parts_num),
    length(trim(trailing '7   ' from p.parts_num)) as "去除右边字符后长度"
from
    product p
where
    p.product_id = "1941573845271945216";

ltrim()去除左空格

select
    p.parts_num as "零件号(包含首尾各3个空格)",
    length(p.parts_num) as "原始长度",
    ltrim(p.parts_num),
    length(ltrim(p.parts_num)) as "去除左空格后长度"
from
    product p
where
    p.product_id = "1941573845271945216";

结果

rtrim()去除右空格

select
    p.parts_num as "零件号(包含首尾各3个空格)",
    length(p.parts_num) as "原始长度",
    rtrim(p.parts_num),
    length(rtrim(p.parts_num)) as "去除右空格后长度"
from
    product p
where
    p.product_id = "1941573845271945216";

结果

上一篇 下一篇


推荐文章

评论
说点什么吧?

发表评论

取消回复
  最新文章