当角色的出现不断变化时,我如何在角色之后获得串联

发布于 2025-01-18 16:39:42 字数 198 浏览 3 评论 0原文

示例

123\.456.578.910.ABC
123\.456.578.910

预期结果

123\.456.578
123\.456.578

对于这两个输入,我应该只得到前 3 个输入,

我尝试了正则表达式、子字符串和指令,但没有得到结果

Example

123\.456.578.910.ABC
123\.456.578.910

Expected result

123\.456.578
123\.456.578

For the both the inputs I should get only the first 3

I tried the regexp and substring and instr but I’m not getting the results

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

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

发布评论

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

评论(3

请远离我 2025-01-25 16:39:43

如果您的价值将始终具有至少3 字符,则可以使用:

SELECT value,
       SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1) AS expected
FROM   table_name;

如果它可能具有较少的情况,并且在这些情况下需要整个字符串:

SELECT value,
       CASE INSTR(value, '.', 1, 3)
       WHEN 0
       THEN value
       ELSE SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1)
       END AS expected
FROM   table_name;

对于您的示例数据:这两个

CREATE TABLE table_name (value) AS
SELECT '123\.456.578.910.ABC' FROM DUAL UNION ALL
SELECT '123\.456.578.910' FROM DUAL;

输出:这两个输出:

value预期
123.456.578.910.abc123.456.578
123.456.578.910123.456.578

db<> fiddle 此处

If you value will always have at least 3 . characters then you can use:

SELECT value,
       SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1) AS expected
FROM   table_name;

If it may have fewer and you want the entire string in those cases then:

SELECT value,
       CASE INSTR(value, '.', 1, 3)
       WHEN 0
       THEN value
       ELSE SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1)
       END AS expected
FROM   table_name;

Which, for your sample data:

CREATE TABLE table_name (value) AS
SELECT '123\.456.578.910.ABC' FROM DUAL UNION ALL
SELECT '123\.456.578.910' FROM DUAL;

Both outputs:

VALUEEXPECTED
123.456.578.910.ABC123.456.578
123.456.578.910123.456.578

db<>fiddle here

落墨 2025-01-25 16:39:42

我们可以在此处使用一个捕获组:

SELECT REGEXP_SUBSTR(col, '^(\d+(\.\d+)*)', 1, 1, NULL, 1)
FROM yourTable;

We can use REGEXP_SUBSTR here with a capture group:

SELECT REGEXP_SUBSTR(col, '^(\d+(\.\d+)*)', 1, 1, NULL, 1)
FROM yourTable;

Demo

弥繁 2025-01-25 16:39:42

传统substr + instr组合是另一个选项:

示例数据:

SQL> with test (col) as
  2    (select '123\.456.578.910.ABC' from dual union all
  3     select '123\.456.578.910'     from dual
  4    )

查询从这里开始:

  5  select col,
  6         substr(col, 1, instr(col, '.', 1, 3) - 1) result
  7  from test;

COL                  RESULT
-------------------- --------------------
123\.456.578.910.ABC 123\.456.578
123\.456.578.910     123\.456.578

SQL>

Traditional, substr + instr combination is another option:

Sample data:

SQL> with test (col) as
  2    (select '123\.456.578.910.ABC' from dual union all
  3     select '123\.456.578.910'     from dual
  4    )

Query begins here:

  5  select col,
  6         substr(col, 1, instr(col, '.', 1, 3) - 1) result
  7  from test;

COL                  RESULT
-------------------- --------------------
123\.456.578.910.ABC 123\.456.578
123\.456.578.910     123\.456.578

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