从varchar中删除其他零

时间:2016-06-01 09:33:08

标签: sql oracle substring

我想从字符串的开头删除所有零0

SQL:

SELECT SUBSTR('00008800000006',0,7)  AS serno ,
       SUBSTR('00008800000006',8,14) AS lot
FROM dual ;

预期结果:

  • SUBSTR(' 00008800000006',0,7)=' 0000880' => Serno =' 880'
  • SUBSTR(' 00008800000006',8,14)=' 0000006' =>很多=' 6'

enter image description here

1 个答案:

答案 0 :(得分:3)

如果您使用的是Oracle,可以试试这个:

select
 REGEXP_REPLACE(SUBSTR('00008800000006',0,7), '^0+(.*$)', '\1') as Serno,
 REGEXP_REPLACE(SUBSTR('00008800000006',8,14), '^0+(.*$)', '\1') as lot
 from dual;

<强> OUPUT

SERNO    LOT

880      6   

您也可以使用LTRIM

select
 ltrim(SUBSTR('00008800000006',0,7), '0') as Serno,
 ltrim(SUBSTR('00008800000006',8,14), '0') as lot
  from dual

<强> OUPUT

SERNO    LOT

880      6