简体   繁体   English

如果是数字则修剪前导零,如果是字母数字则修剪不零。

[英]Trim leading zeroes if it is numeric and not trim zeroes if it is alphanumeric

In a column, there are numeric and alphanumeric values starting with '0'. 在一列中,存在以“ 0”开头的数字和字母数字值。 How to trim leading zeroes if it is numeric and should not trim zeroes if it is alphanumeric in Oracle. 在Oracle中,如何修剪前导零(如果是数字),以及如何修剪零(如果是字母数字)。

I need to use it in WHERE Condition . 我需要在WHERE条件下使用它。

Ex. 例如

000012345 should be 12345 . 000012345应该是12345 012321 should be 12321 . 012321应该是12321 00012JY12 should be 00012JY12 . 00012JY12应该是00012JY12

This is what I tried: 这是我尝试的:

    SELECT COUNT(*)
    FROM <TABLE 1> ONN, <TABLE 2> SV
   WHERE SV.CSA_SHP_VISIT_STG_SEQ_ID=ONN.CSA_SHOP_VIST_SEQ_ID
    AND EXISTS (SELECT '1' FROM  <TABLE 3> TMP 
    WHERE TRIM(SV.WORK_ORDER_NUM) = TRIM(TMP.WORK_ORDER_NUM)
    AND PLANT IN ('EMA')
    AND regexp_replace(TRIM(ONN.INSTLD_PART), '^0+([[:digit:]]+)$', 
   '\1')=TRIM(TMP.INSTLD_PART)  AND
  TRIM(ONN.INSTLD_PART_SERIAL_NUM)=TRIM(TMP.INSTLD_PART_SERIAL_NUM) AND      
    nvl(to_number(TRIM(ONN.INSTLD_PART_CSN)),0)=
    nvl(to_number(TRIM(TMP.INSTLD_PART_CSN)),0)
    and REGEXP_LIKE(tmp.INSTLD_PART_CSN, '^-?\d+(\.\d+)?$'))

Whenever possible (in this case it is), use standard string functions, such as SUBSTR, INSTR, TRANSLATE, etc. instead of regular expression functions. 尽可能(在这种情况下)使用标准字符串函数(例如SUBSTR,INSTR,TRANSLATE等),而不使用正则表达式函数。 Regular expressions are much more powerful, but also much more time consuming (precisely for that reason), so they should be used only when really needed. 正则表达式功能更强大,但也更耗时(正是因为这个原因),因此仅在真正需要时才使用它们。

If the column name is str , then: 如果列名是str ,则:

case when translate(str, 'z0123456789', 'z') is null
     then ltrim(str, '0')
     else str                      end

TRANSLATE will translate z to itself, all the digits to NULL, and all other characters to themselves. TRANSLATE将z转换为自身,将所有数字转换为NULL,并将所有其他字符转换为自身。 (Alas, the z, or SOME non-digit character, is needed.) (可惜,z或某些非数字字符是必需的。)

The input is all-digits if and only if the result of TRANSLATE is NULL. 当且仅当TRANSLATE的结果为NULL时,输入才为全数字。

Demo: 演示:

select str, case when translate(str, 'z0123456789', 'z') is null
                 then ltrim(str, '0')
                 else str
            end  as new_str
from
(
  select '000012345' as str from dual union all
  select '012321'    as str from dual union all
  select '00012JY12' as str from dual
);

STR       NEW_STR 
--------- ---------
000012345 12345    
012321    12321    
00012JY12 00012JY12

Use regexp_replace(col, '^0+([[:digit:]]+)$', '\\1') . 使用regexp_replace(col, '^0+([[:digit:]]+)$', '\\1')

This replaces strings that only consist of leading zeros and trailing digits with the trailing digits alone, thus removing the zeros. 这将仅由前导零和尾随数字组成的字符串替换为仅尾随数字,从而删除了零。

Sample query: 查询样例:

select col, regexp_replace(col, '^0+([[:digit:]]+)$', '\1') as newvalue
from
(
  select '000012345' as col from dual
  union all
  select '012321' as col from dual
  union all
  select '00012JY12' as col from dual
);

Result: 结果:

COL       | NEWVALUE
----------+----------
000012345 | 12345
012321    | 12321
00012JY12 | 00012JY12

If you are using Oracle 12.2, you could use the error handling of the CAST expression. 如果使用的是Oracle 12.2,则可以使用CAST表达式的错误处理。

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/CAST.html#GUID-5A70235E-1209-4281-8521-B94497AAEF75 https://docs.oracle.com/zh_CN/database/oracle/oracle-database/12.2/sqlrf/CAST.html#GUID-5A70235E-1209-4281-8521-B94497AAEF75

Along that: 伴随着:

CASE WHEN CAST(<expression> AS NUMERIC DEFAULT NULL ON CONVERSION ERROR) IS NULL
     THEN <expression>
     ELSE TRIM(LEADING '0' FROM <expression>)
 END

Replace <expression> with your column name (or whatever). <expression>替换为您的列名(或其他名称)。 I'm using null as magic value in the default null on conversion error clause but this does no harm as null input will then just match the THEN clause and pass the null through. default null on conversion error子句的default null on conversion error中将null用作魔术值,但这无害,因为null输入将仅匹配THEN子句并通过null

You can create a function that check if it is numeric or not, see this link for the function sample, 您可以创建一个函数来检查它是否为数字,请参见此链接以获取函数示例,

check if "it's a number" function in Oracle 检查Oracle中“是否为数字”功能

You can TRIM the zeros of numeric value just by adding 0 to it, 您只需将数字加零即可对数字的零进行微调,

Sample code: 样例代码:

--get  the IS_NUMERIC function from the link 

SELECT DECODE(IS_NUMERIC(col1), 'Y', col1+0, 'N', col1) 
  FROM your_table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM