3

I am getting the following error on the oracle query below. I'm not seeing an issue with the format that I am giving in the query. I'm thinking it might have something to do with the .SSS but I can't be certain:

SELECT
     *   
 FROM
     (   
         SELECT
             *   
         FROM
             comprater_requests
         WHERE
             spname =?
             AND effectivedate >= TO_DATE(?,'yyyy-MM-dd HH:mi:ss.SSS')
             AND effectivedate <= TO_DATE(?,'yyyy-MM-dd HH:mi:ss.SSS')
     )   
 WHERE
     ROWNUM <= 100 

Error:

ORA-01810: format code appears twice

Parameters:

Parameters: [Google, 2018-07-24 00:00:00.000, 2018-09-06 00:00:00.000]

Table:

    CREATE TABLE COMPRATER_REQUESTS
(
  ID                    NUMBER DEFAULT COMP_RATER_SEQ.NEXTVAL PRIMARY KEY,
  TRANSACTIONID         VARCHAR2(20 BYTE) NOT NULL,
  QUOTE                 CLOB,
  ARCHIVEXML            CLOB,
  ADDITIONALINFO        CLOB,
  QUOTEID               VARCHAR2(20 BYTE),
  AGENTID               CHAR(50 BYTE),
  EFFECTIVEDATE         DATE
);

The user selects two dates from the UI which is sent to the back end code in the following format 'Tue Sep 04 00:00:00 EDT 2018' but the oracle DB has the dates stored as '2018-09-04 00:00:00.0'. So I tried converting the date they select to that format by doing:

 dateFormat = new SimpleDateFormat(CompRaterPropertiesML.ACORD_DB_DATE_FORMAT)
 fromDateFormat = dateFormat.format(selectedDate1)
 toDateFormat = dateFormat.format(selectedDate2)

This converts it to the format I need but it is now a string which I believe is giving me the issue.

iceMan33
  • 359
  • 2
  • 16

3 Answers3

2

TO_DATE converts to a date, not to a timestamp.

In Oracle, dates do not have fractions of a second. What you want to create are timestamps; for that, use TO_TIMESTAMP.

After you do, you will get the same error; to indicate fractions of a second, use .ff, not .sss. (NOTE: Two f's, not three!)

You are also missing 24 after hh. Should be '... hh24:mi:ss.ff'

1

You are using parameters. Don't pass the values in as strings -- use the appropriate data type:

SELECT *   
FROM (SELECT cr.*   
      FROM comprater_requests cr
      WHERE spname = ? AND
            effectivedate >= ? AND
            effectivedate <= ?
     ) cr 
 WHERE ROWNUM <= 100 ;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

I actually figured out the issue.

The Date that was stored in the oracle DB was different from the format that was being passed into the query to perform the search. So I changed the format of the parameters to be a string in the format of 'YYYY-MM-DD' and then changed the format of the EFFECTIVEDATE with TO_CHAR. So my query looks like the following:

SELECT * FROM ( SELECT * FROM COMPRATER_REQUESTS WHERE SPNAME = ? AND TO_CHAR(EFFECTIVEDATE,'yyyy-mm-dd') >= ? AND TO_CHAR(EFFECTIVEDATE,'yyyy-mm-dd') <= ? ) WHERE ROWNUM <= 100
iceMan33
  • 359
  • 2
  • 16