3

I have the following example

05.04.2018 at 11:10:37 AEST

My goal is to remove all alpha chars from the string. The expected result is to remove the ' at' and ' AEST' sub-strings:

Note: There should be only one space between the date and the time. There should be no space at the end of the string

05.04.2018 11:10:37

The 'AEST' sub-string is a timezone and can change.

This is my current SQL query:

select SUBSTR(REGEXP_REPLACE(REGEXP_REPLACE('05.04.2018 at 11:10:37 AEST',' at',''), ' EST| AEDT| AEST', ''),1) from dual;

I'm looking to enhance my query (preferably using regex) so I will not have to specify explicitly all potential values for timezone (as currently being done in the query)

Thanks

4 Answers 4

3

You may use \s*[a-zA-Z]+ / \s*[[:alpha:]]+ regex:

select REGEXP_REPLACE('05.04.2018 at 11:10:37 AEST','\s*[a-zA-Z]+','') as Result from dual

The pattern matches

  • \s* - 0+ whitespace chars
  • [a-zA-Z]+ - 1+ ASCII letters ([[:alpha:]]+ will match any letters).

See an online Oracle demo. Output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

PERFECT. Thanks!
3

Something like this?

SQL> with test as (select '05.04.2018 at 11:10:37 AEST' col from dual)
  2  select regexp_replace(col, '\s*[[:alpha:]]+') result
  3  from test;

RESULT
-------------------
05.04.2018 11:10:37

SQL>

Comments

1

You can use:

select trim(regexp_replace(col, '[a-zA-Z]', ''))

I assume you want to remove the final space as well.

1 Comment

Thanks. That approach leaves double space between the date and time
1

Keep it simple! Why not without regexp? The date part and the time part are always at the same position.

select substr(col,1,10)  -- the date part
    ||' '||              -- the blank
    substr(col,15,8)     -- the time part
from tab;

e.g.

SQL> select substr(col,1,10)  
        ||' '||
        substr(col,15,8)  "date+time"   
     from (
        select '05.04.2018 at 11:10:37 AEST' col
            from dual) tab; 

date+time
-------------------
05.04.2018 11:10:37

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.