首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Reg Exp replace以匹配oracle中的hh:mm格式

Reg Exp replace以匹配oracle中的hh:mm格式
EN

Stack Overflow用户
提问于 2021-09-08 16:05:44
回答 1查看 52关注 0票数 0

使用regexp replace编写oracle查询,以修复空间并返回hh:mm格式。

代码语言:javascript
运行
复制
with
  test_data (srt_tm) as (
    select '1:00'  from dual union all
    select '01:00' from dual union all
    select ' 01:00' from dual union all
    select '4:00'  from dual union all
    select '04:00' from dual union all
    select ' 04:00' from dual
  )

预期输出:

已尝试查询:

代码语言:javascript
运行
复制
select (strt_tm,regexp_replace(strt_tm,'^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$','hh:mm') as "REPLACE" 
from test_data
EN

Stack Overflow用户

发布于 2021-09-08 16:49:56

只需在小时数中添加额外的0,并删除除最后两位以外的所有数字:

代码语言:javascript
运行
复制
select 
  strt_tm,
  regexp_replace(
     regexp_replace(
        strt_tm
       ,'(\d+):'
       ,'0\1:'
     )
   ,'.*(\d{2}:)'
   ,'\1'
  ) as "REPLACE" 
from test_data;

完整的test_data示例:

代码语言:javascript
运行
复制
with
  test_data (strt_tm) as (
    select '1:00'  from dual union all
    select '01:00' from dual union all
    select ' 01:00' from dual union all
    select '4:00'  from dual union all
    select '04:00' from dual union all
    select ' 04:00' from dual
  )
select 
  strt_tm,
  regexp_replace(
     regexp_replace(
        strt_tm
       ,'(\d+):'
       ,'0\1:'
     )
   ,'.*(\d{2}:)'
   ,'\1'
  ) as "REPLACE" 
from test_data
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69106385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档