简体   繁体   中英

No, I can't change the field

I am working with a database that I am a lowly peon. I cannot make changes to the database and my suggestions are simply ignored. The field that I am interested in is a varchar that captures the time in a 'MMDDYYHH24MISS' format but, as I said, stores it as text in a varchar field.

My Boss wants a report that I have to use a between statement on this field to filter an actual DATE range. Well, here is the problem, the text stores 0000 hours as 2400 hours and this really screws up the BETWEEN clause. Still, I cannot change the field type.

I have figured out how to replace the 24 to 00 but how do I get this to run the BETWEEN in the WHERE section of the SELECT statement? Everyone says “just change the VARCHAR field to a DATA field.” Well, I can't – I am a lowly peon.

How would I run this code and then run another SELECT statement referencing the FIX_DATETIME in a BETWEEN statement in the WHERE section of the SELECT?

A young lady wading in a swamp of data, Copra Loom

Ie. WHERE to_date(FIX_DATETIME, 'MMDDYYHH24MISS') BETWEEN (sysdate-90) and sysdate

Here is my code:

SELECT MOPACTIVITY.MOPID,
    (CASE 
        WHEN SUBSTR(MOPACTIVITY.MOPID, 7, 2) = '24' THEN SUBSTR(MOPACTIVITY.MOPID, 1, 2)||SUBSTR(MOPACTIVITY.MOPID, 3, 2)||SUBSTR(MOPACTIVITY.MOPID, 5, 2)||'00'||SUBSTR(MOPACTIVITY.MOPID, 9, 2)||SUBSTR(MOPACTIVITY.MOPID, 11, 2)
        ELSE MOPACTIVITY.MOPID
    END ) FIX_DATETIME,
sysdate "SYSDATE"
FROM MOPUSER.MOPACTIVITY

This will turn the 24 into 00:

SUBSTR(FIX_DATETIME,1,6) 
   || REPLACE(SUBSTR(FIX_DATETIME,7,2),'24','00') 
   || SUBSTR(FIX_DATETIME,9,4)

Then your BETWEEN test is:

WHERE to_date (SUBSTR(FIX_DATETIME,1,6) 
                  || REPLACE(SUBSTR(FIX_DATETIME,7,2),'24','00') 
                  || SUBSTR(FIX_DATETIME,9,4),
              'MMDDYYHH24MISS') BETWEEN (sysdate-90) and sysdate

The solution that I have gone with is to make a field that strips out the 24 and replace them with 00. Run the table out of SQuirreL and into TEMP table and run the between from my temporary table. That's all I got :/

XOXO,

Copra

DROP TABLE TEMPXMOP24;

CREATE TABLE TEMPXMOP24
(
   MOPID VARCHAR2(12),
   TYPE VARCHAR2(200),
   MOPSTART DATE,
   MOPAPPROVEDTIME DATE,
   TIME_DIF VARCHAR2(45),
   FIXED_MOP VARCHAR2(12)
);

INSERT INTO TEMPXMOP24 (MOPID,TYPE,MOPSTART,MOPAPPROVEDTIME,TIME_DIF,FIXED_MOP)
SELECT MOPACTIVITY.MOPID,
MOPACTIVITY.MOPSERVICEIMPACTED "TYPE",
MOPACTIVITY.MOPSTART,
MOPACTIVITY.MOPAPPROVEDTIME,
ROUND(MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME,2)||' days' "TIME_DIF",
    (CASE 
        WHEN SUBSTR(MOPACTIVITY.MOPID, 7, 2) = '24' THEN SUBSTR(MOPACTIVITY.MOPID, 1, 6)||'00'||SUBSTR(MOPACTIVITY.MOPID, 9, 4)
        ELSE MOPACTIVITY.MOPID
    END ) FIXED_MOP
FROM MOPUSER.MOPACTIVITY
WHERE MOPACTIVITY.MOPSTART -  MOPACTIVITY.MOPAPPROVEDTIME BETWEEN 0 and 14;

SELECT * FROM TEMPXMOP24
WHERE TRUNC(TO_DATE(TEMPXMOP24.FIXED_MOP, 'MMDDYYHH24MISS'))
  BETWEEN TRUNC(SYSDATE-90) AND TRUNC(SYSDATE)

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