简体   繁体   中英

ORACLE SQL Trim ineffective with Regular Expressions

CLARIFICATION

When I simplify the query , ie

   SELECT TO_CHAR(MIN(I.INCIDENTID))               AS "Incident ID",
          TRIM(TO_CHAR(I.CREATIONDATE,'DD-MON-YYYY'))           AS "Creation Date"    
   FROM   INCIDENT I  
   GROUP BY TRIM(TO_CHAR(I.CREATIONDATE,'DD-MON-YYYY'))

I get

在此处输入图片说明

But if I incorporate into my actual query for 164,000+ distinct rows, I get

在此处输入图片说明

But I expect this (only difference is, Creation Date must have proper Date format, not this complex string)

在此处输入图片说明

ORIGINAL QUESTION

I am reading a date-time from an external Oracle Database, and have trimmed any extra spaces successfully with TRIM(I.CREATIONDATE) .

I have verified this because my SQL query only displays distinct date values

However, TRIM(I.CREATIONDATE) turns 2/26/2019 11:05:44 AM into 26-FEB-19 11.05.43.925000 AM , but I only want 26-FEB-19

When I apply regular expression to only get date, ie REGEXP_SUBSTR(TRIM(I.CREATIONDATE),'[^ ]+') , it certainly outputs 26-FEB-19 , but somehow extra spaces are added because I get duplicate dates.

I have tried applying TRIM a second time, ie TRIM(REGEXP_SUBSTR(TRIM(I.CREATIONDATE),'[^ ]+')) , but I still get duplicates

Then I tried running regular expression first, then trim, but this does not work, ie TRIM(REGEXP_SUBSTR(I.CREATIONDATE,'[^ ]+')) still gives duplicates.

Please assist

I think you want trunc() , not trim() to remove the time component.

So try:

trunc(i.creationdate)

Or if you want the days in a particular string representation:

to_char(i.creationdate, 'YYYY-MM-DD')

尝试to_char:

trim(to_char(column_name,'dd-mon-yy'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM