我如何根据 mysql 中字符串的长度 trim 前导零

How can i trim leading zero based on the length of the string in mysql

我的 table 中有一个名为 UPC 的列。该列最多允许 13 digits.For 例如列值应该是这样的。

0892902000161
0097512135764
4046228003979

现在我需要 trim 只有一个零,如果它有像​​这样的前导两个零 0097512135764。现在我正在使用下面的查询来 trim 所有前导零。

SELECT TRIM(LEADING '0' FROM `UPC`) upc FROM `mytable`.

但现在我只需要 trim 一个零,如果它有两个前导 zeros.Some 一个可以指导 me.Any 帮助将不胜感激。

SELECT 
  CASE WHEN UPC LIKE '00%' THEN RIGHT(UPC, LENGTH(UPC) -1)
  ELSE UPC
  END AS UPC
FROM `mytable`

in the same query how can we check the count of upc number? I mean if length of upc is 13 
digits and first one is zero then need to remove first zero. Then upc length should be 12. 
If upc length is 13 and first two are zeros then need to remove single zero. 
Then also upc length should be 12 digits.How can we acheive this?

CASE 应该足够了。对于以 00... 开头的 UPC,得到没有第一个字符的 UPC

SELECT 
  CASE WHEN UPC LIKE '00%' THEN RIGHT(UPC, LENGTH(UPC) -1)
  ELSE UPC
  END AS UPC
FROM `mytable`

LiveDemo

编辑:

If upc length is 13 and first two are zeros then need to remove single zero.

If length of upc is 13 digits and first one is zero then need to remove first zero.

SELECT 
  CASE 
   WHEN UPC LIKE '00%' AND LENGTH(UPC) = 13 THEN RIGHT(UPC, LENGTH(UPC) -1)
   WHEN UPC LIKE '0%' AND LENGTH(UPC) = 13 THEN RIGHT(UPC, LENGTH(UPC) -1)
  ELSE UPC
  END AS UPC
FROM `mytable`

LiveDemo