提取Oracle中的日期以及上午或下午。

huangapple go评论69阅读模式
英文:

Extract Date Along with Am or pm in oracle

问题

我想从给定的字符串 Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv 中获取时间模式以及上午或下午(AM或PM)。

我尝试了以下方法:

Select regexp_substr(filename,'[0-9]{4}[APM]+',1,3)
From
(Select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv' filename from dual);

这样只会给我最后的数字,例如 0329,但我需要 0329PM

英文:

I want to get the time pattern along with AM or PM from the given string Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv

I tried the following:

Select regexp_substr(filename,'\d{4}',1,3)
From
(Select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv' filename from dual);

which only gives me the last number, e.g. 0329, but I need 0329PM.

答案1

得分: 2

Using this form of REGEXP_SUBSTR() will get what you need in one call. It returns the first group, which is the set of characters after the last underscore and before the literal period of 1 or more numbers followed by an A or P then an M.

with tbl(filename) as (
Select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv'
from dual
)
select regexp_substr(filename, '_(\d+[AP]M).', 1, 1, NULL, 1)
From tbl;

Actually, to tighten up the match you could make it case-insensitive and add the extension:

select regexp_substr(filename, '_(\d+[AP]M).csv', 1, 1, 'i', 1)
From tbl;

Note if a match is not found NULL will be returned.

英文:

Using this form of REGEXP_SUBSTR() will get what you need in one call. It returns the first group, which is the set of characters after the last underscore and before the literal period of 1 or more numbers followed by an A or P then an M.

with tbl(filename) as (
  Select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv'  
  from dual
)
select regexp_substr(filename, '_(\d+[AP]M)\.', 1, 1, NULL, 1) 
From tbl;

Actually, to tighten up the match you could make it case-insensitive and add the extension:

select regexp_substr(filename, '_(\d+[AP]M)\.csv', 1, 1, 'i', 1) 
From tbl;

Note if a match is not found NULL will be returned.

答案2

得分: 0

Nested substr is one option (if data always looks like this; you didn't say it doesn't):

SQL> with test (col) as
  2    (select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv' from dual)
  3  select substr(substr(col, -10), 1, 6) result from test
  4  /

RESULT
------
0329PM

SQL>;
  • the inner substr returns the last 10 characters (0329PM.csv)
  • the outer substr returns the first 6 characters out of it (0329PM)

Or, using regular expressions:

SQL> with test (col) as
  2    (select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv' from dual)
  3  select regexp_substr(translate(col, '_.', '  '), '\S+',
  4                       1,
  5                       regexp_count(translate(col, '_.', '  '), '\S+') - 1
  6                      ) result
  7  from test;

RESULT
------
0329PM

SQL>;
  • line #3: translate replaces underlines and dots with a space
  • line #4: start from the beginning
  • line #5: return substring which is one before the last one
英文:

Nested substr is one option (if data always looks like this; you didn't say it doesn't):

SQL> with test (col) as
  2    (select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv' from dual)
  3  select substr(substr(col, -10), 1, 6) result from test
  4  /

RESULT
------
0329PM

SQL>
  • the inner substr returns the last 10 characters (0329PM.csv)
  • the outer substr returns the first 6 characters out of it (0329PM)

Or, using regular expressions:

SQL> with test (col) as
  2    (select 'Aaaaa_gggg_ne_A030_66788_Abcd_Oct_24_0329PM.csv' from dual)
  3  select regexp_substr(translate(col, '_.', '  '), '\S+',
  4                       1,
  5                       regexp_count(translate(col, '_.', '  '), '\S+') - 1
  6                      ) result
  7  from test;

RESULT
------
0329PM

SQL>
  • line #3: translate replaces underlines and dots with a space
  • line #4: start from the beginning
  • line #5: return substring which is one before the last one

huangapple
  • 本文由 发表于 2020年1月3日 21:30:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定