Oracle 内置字符串字符类

发布于 2024-07-26 18:09:09 字数 326 浏览 5 评论 0原文

Oracle 是否有内置的字符串字符类常量(数字、字母、字母、大写、小写等)?

我的实际目标是有效地仅从现有字符串中返回数字 [0-9]。

不幸的是,我们仍然使用 Oracle 9,因此正则表达式在这里不是一个选项。

示例 该字段应包含零到三个字母、3 或 4 个数字,然后是零到两个字母。 我想提取数字。

String    --> Result
ABC1234YY --> 1234
D456YD    --> 456
455PN     --> 455

Does Oracle have built-in string character class constants (digits, letters, alphanum, upper, lower, etc)?

My actual goal is to efficiently return only the digits [0-9] from an existing string.

Unfortunately, we still use Oracle 9, so regular expressions are not an option here.

Examples
The field should contain zero to three letters, 3 or 4 digits, then zero to two letters. I want to extract the digits.

String    --> Result
ABC1234YY --> 1234
D456YD    --> 456
455PN     --> 455

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁以何悠 2024-08-02 18:09:09

没有字符串常量,但您可以这样做:

select translate
      ( mystring
      , '0'||translate (mystring, 'x0123456789', 'x')
      , '0'
      )
from mytable;

例如:

select translate
      ( mystring
      , '0'||translate (mystring, 'x0123456789', 'x')
      , '0'
      )
from
( select 'fdkhsd1237ehjsdf7623A@L:P' as mystring from dual);

TRANSLAT
--------
12377623

如果您想经常这样做,您可以将其包装为函数:

create function only_digits (mystring varchar2) return varchar2
is
begin
   return
      translate
         ( mystring
         , '0'||translate (mystring, 'x0123456789', 'x')
         , '0'
         );
end;
/

然后:

SQL> select only_digits ('fdkhsd1237ehjsdf7623A@L:P') from dual;

ONLY_DIGITS('FDKHSD1237EHJSDF7623A@L:P')
-----------------------------------------------------------------
12377623

No string constants, but you can do:

select translate
      ( mystring
      , '0'||translate (mystring, 'x0123456789', 'x')
      , '0'
      )
from mytable;

For example:

select translate
      ( mystring
      , '0'||translate (mystring, 'x0123456789', 'x')
      , '0'
      )
from
( select 'fdkhsd1237ehjsdf7623A@L:P' as mystring from dual);

TRANSLAT
--------
12377623

If you want to do this often you can wrap it up as a function:

create function only_digits (mystring varchar2) return varchar2
is
begin
   return
      translate
         ( mystring
         , '0'||translate (mystring, 'x0123456789', 'x')
         , '0'
         );
end;
/

Then:

SQL> select only_digits ('fdkhsd1237ehjsdf7623A@L:P') from dual;

ONLY_DIGITS('FDKHSD1237EHJSDF7623A@L:P')
-----------------------------------------------------------------
12377623
陌伤浅笑 2024-08-02 18:09:09

您可以检查 Oracle 上预定义数据类型的列表 在这里,但你不会找到你要找的东西。

要提取字符串的数字,您可以使用以下函数的某种组合:

如果您提供更简洁的示例将更容易给您详细的解决方案。

You can check the list for predefined datatypes on Oracle here, but you are not going to find what are you looking for.

To extract the numbers of an string you can use some combination of these functions:

If you provide a more concise example will be easier to give you a detailed solution.

夜唯美灬不弃 2024-08-02 18:09:09

If you are able to use PL/SQL here, another approach is write your own regular expression matcher function. One starting point is Rob Pike's elegant, very tiny regular expression matcher in Chapter 1 of Beautiful Code. One of the exercises for the reader is to add character classes. (You'd first need to translate his 30 lines of C code into PL/SQL.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文