Home » SQL & PL/SQL » SQL & PL/SQL » ORA-01840: input value not long enough fro date format. (Oracle 11 g)
ORA-01840: input value not long enough fro date format. [message #597674] Mon, 07 October 2013 09:18 Go to next message
jayant.tripathi
Messages: 8
Registered: October 2013
Junior Member
Hi,

I am executing an SQL query using a JAVA program. This is running fine for all the dates except last day of any month.
When i enter 20130205 it is working fine but for 30 or 31, it is throwing this exception.

Format of the date is yyyymmdd.

Can you please help me with this?
SELECT DISTINCT :SPID, t1.nodename nodename, t1.gdate, t1.gtime,
                   t1.gdate_time, t1.ndd_mw, t1.ndd_mv, nvl(t1.fms_mw,0)+t2.fms_mw+mw, nvl(t1.fms_mv,0)+t2.fms_mv+mv,
                   t1.nddraw_mw, t1.nddraw_mv
              FROM tmp_ndd_nddraw_fms_data t1, tmp_ndd_nddraw_fms_data t2, (
              WITH min_max_date as(select min(gdate) min_date,max(gdate) max_date from tmp_ms_data),
maps as( select game from unmetmap where gspid=:SPID),
t AS
     (
        SELECT   TO_CHAR (f.target_datetime, 'YYYYmmdd') gdate,
                 TO_CHAR (f.target_datetime, 'HH24mi') gtime,
                 SUM (f.mean_frcstd_mw) mw
            FROM pkd_dt f, wgen w,maps m,min_max_date d
           WHERE f.gen_id = w.gen_id
             AND w.gen_name =m.gen_name
             AND f.target_datetime BETWEEN   TO_DATE (d.min_date,
                                                      'yyyymmddhh24mi'
                                                     )
                                           - 1 / 48
                                       AND   TO_DATE (d.max_date + 1,
                                                      'yyyymmddhh24mi'
                                                     )
                                           + 1 / 48
        GROUP BY f.target_datetime
        ORDER BY TO_CHAR (f.target_datetime, 'YYYYmmdd'),
                 TO_CHAR (f.target_datetime, 'HH24mi')),
     m_w AS
     (
        SELECT gdate,
               DECODE (gtime,
                       2330, 2400,
                       TO_CHAR (  TO_DATE (LPAD (gtime, 4, '000'), 'HH24mi')
                                + 1 / 48,
                                'HH24MI'
                               )
                      ) gtime,
               mw
          FROM (SELECT gdate, gtime + 0 gtime, mw
                  FROM t t1
                UNION
                SELECT t1.gdate, t1.gtime + 30 gtime, (t1.mw + t2.mw) / 2
                  FROM t t2, t t1
                 WHERE (t1.gdate = t2.gdate AND t1.gtime + 100 = t2.gtime)
                    OR (    t1.gdate + 1 = t2.gdate
                        AND t1.gtime = 2300
                        AND t2.gtime = 0
                       )),min_max_date
         WHERE gdate BETWEEN min_date AND max_date),
     t2 AS
     (
        SELECT   TO_CHAR (f.target_datetime, 'YYYYmmdd') gdate,
                 TO_CHAR (f.target_datetime, 'HH24mi') gtime,
                 SUM (f.f_mvar) mw
            FROM extdata f, wger w,maps m,min_max_date d
           WHERE f.gen_id = w.gen_id
             AND w.gen_name =m.gen_name
             AND f.target_datetime BETWEEN   TO_DATE (min_date,
                                                      'yyyymmddhh24mi'
                                                     )
                                           - 1 / 48
                                       AND   TO_DATE (max_date + 1,
                                                      'yyyymmddhh24mi'
                                                     )
                                           + 1 / 48
        GROUP BY f.target_datetime
        ORDER BY TO_CHAR (f.target_datetime, 'YYYYmmdd'),
                 TO_CHAR (f.target_datetime, 'HH24mi')),
     wind_mv AS
     (
        SELECT gdate,
               DECODE (gtime,
                       2330, 2400,
                       TO_CHAR (  TO_DATE (LPAD (gtime, 4, '000'), 'HH24mi')
                                + 1 / 48,
                                'HH24MI'
                               )
                      ) gtime,
               mw mv
          FROM (SELECT gdate, gtime + 0 gtime, mw
                  FROM t2 t1
                UNION
                SELECT t1.gdate, t1.gtime + 30 gtime, (t1.mw + t2.mw) / 2
                  FROM t2 t2, t2 t1
                 WHERE (t1.gdate = t2.gdate AND t1.gtime + 100 = t2.gtime)
                    OR (    t1.gdate + 1 = t2.gdate
                        AND t1.gtime = 2300
                        AND t2.gtime = 0
                       )),min_max_date
         WHERE gdate BETWEEN min_date AND max_date)
     (SELECT wmw.gdate, wmw.gtime, nvl(mw,0)mw, nvl(mv,0) mv
        FROM w_mw, w_mv
       WHERE wind_mw.gdate = wind_mv.gdate(+) AND wind_mw.gtime = wind_mv.gtime(+))) T3
             WHERE t1.gdate = t2.gdate
               AND t1.gtime = t2.gtime
               and t1.gtime=t3.gtime
               and t1.gdate=t3.gdate
               AND t1.nodename = t2.nodename || 'DW'
               AND t1.nodename LIKE '%DW'
               AND t1.spid = 0

*BlackSwan added {code} tags. Please do so yourself in the future.

[Updated on: Mon, 07 October 2013 09:22] by Moderator

Report message to a moderator

Re: ORA-01840: input value not long enough fro date format. [message #597676 is a reply to message #597674] Mon, 07 October 2013 09:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/


which line throws the error?
Re: ORA-01840: input value not long enough fro date format. [message #597677 is a reply to message #597676] Mon, 07 October 2013 09:42 Go to previous messageGo to next message
jayant.tripathi
Messages: 8
Registered: October 2013
Junior Member
Hi, Thanks fro your email

I am not sure which line throws the error. I am using JSP front end to enter the dates and then click a button. After click, a logic is executed where this query runs.

Thanks.
Re: ORA-01840: input value not long enough fro date format. [message #597678 is a reply to message #597677] Mon, 07 October 2013 09:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I am not sure which line throws the error.
Neither are we.

since we don't have your tables or data, we can not run, test or debug posted code.
COPY the SQL & PASTE into sqlplus to learn which line throws the error.
Re: ORA-01840: input value not long enough fro date format. [message #597679 is a reply to message #597677] Mon, 07 October 2013 09:52 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You need to break that query into chunks and work out which bit throws the error.
You've got a large query there that we can't run with multiple date conversions.
I am very suspicious of this bit though:
TO_DATE (max_date + 1,
                                                      'yyyymmddhh24mi'
                                                     )

what datatype is max_date?
Re: ORA-01840: input value not long enough fro date format. [message #597684 is a reply to message #597679] Mon, 07 October 2013 10:17 Go to previous messageGo to next message
jayant.tripathi
Messages: 8
Registered: October 2013
Junior Member
Hi,

the datatype of Max_date is NUMBER.

thanks.
icon13.gif  Re: ORA-01840: input value not long enough fro date format. [message #597685 is a reply to message #597674] Mon, 07 October 2013 10:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
When i enter 20130205 it is working fine but for 30 or 31, it is throwing this exception.

Quote:
the datatype of Max_date is NUMBER.


When you store date in numbers you have these errors and can't avoid them.
Garbage in garbage out.

Change "TO_DATE (max_date + 1, 'yyyymmddhh24mi')" by "TO_DATE (to_char(max_date,'fm000000000000'), 'yyyymmddhh24mi') + 1"

Change your model to store dates in DATE column and nothing else.


[Updated on: Mon, 07 October 2013 10:27]

Report message to a moderator

Re: ORA-01840: input value not long enough fro date format. [message #597687 is a reply to message #597685] Mon, 07 October 2013 10:33 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Michel Cadot wrote on Mon, 07 October 2013 16:25

Change "TO_DATE (max_date + 1, 'yyyymmddhh24mi')" by "TO_DATE (to_char(max_date,'fm000000000000'), 'yyyymmddhh24mi') + 1"

Given the format mask, missing leading zeros shouldn't be an issue unless the dates pre-date 1000 A.D.

on the other hand:

SQL> SELECT TO_DATE (201310071259, 'yyyymmddhh24mi') FROM dual;
 
TO_DATE(201310071259,'YYYYMMDD
------------------------------
07/10/2013 12:59:00
 
SQL> SELECT TO_DATE (201310071259 + 1, 'yyyymmddhh24mi') FROM dual;
 
SELECT TO_DATE (201310071259 + 1, 'yyyymmddhh24mi') FROM dual
 
ORA-01851: minutes must be between 0 and 59
 
SQL> 


That code is a bug waiting to happen, but I don't think it's the bug the OP is currently hitting.
Re: ORA-01840: input value not long enough fro date format. [message #597689 is a reply to message #597687] Mon, 07 October 2013 10:56 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Another reason not to use dates as numbers or strings:
t1.gdate + 1 = t2.gdate

So the above theoretically matches a date to the following day. What happens if gdate is the last day in the month:
SQL> SELECT TO_CHAR (last_day(SYSDATE), 'YYYYmmdd') + 1 FROM dual;
 
TO_CHAR(LAST_DAY(SYSDATE),'YYY
------------------------------
                      20131032
 
SQL> 

That's not going to work is it.

You need to rewrite this from scratch using dates as dates.
If the client can't cope with dates then convert them to char as the very last step. The where clause needs to check dates as dates.
Re: ORA-01840: input value not long enough fro date format. [message #597690 is a reply to message #597689] Mon, 07 October 2013 11:04 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
To put it another way, this:
WHERE (t1.gdate = t2.gdate AND t1.gtime + 100 = t2.gtime)
       OR (t1.gdate + 1 = t2.gdate
           AND t1.gtime = 2300
           AND t2.gtime = 0
          )

Is trying to match up two records where the date time is 1 hour apart. It is slow, bug prone and the code is confusing.
If gdate was an actual date time it would simply be this:
WHERE t1.gdate + 1/24 = t2.gdate

Which is easier to read, more efficient and will always work.
Re: ORA-01840: input value not long enough fro date format. [message #597755 is a reply to message #597690] Tue, 08 October 2013 05:26 Go to previous messageGo to next message
jayant.tripathi
Messages: 8
Registered: October 2013
Junior Member
Hi
Thanks for your responses.

I have tried TO_DATE (to_char(max_date,'fm000000000000'), 'yyyymmddhh24mi') + 1" but it did not work.

ANy other suggestion. Writing whole logic again is not possble at the moment by taking dates as DATE. Tat table is being used by several fucntionalities. SO how to cope up with this NUMBER thing?

Thanks.
icon13.gif  Re: ORA-01840: input value not long enough fro date format. [message #597756 is a reply to message #597755] Tue, 08 October 2013 05:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
but it did not work.


Which help us in any way to know why "it did not work".
Any other information?

Re: ORA-01840: input value not long enough fro date format. [message #597766 is a reply to message #597755] Tue, 08 October 2013 07:00 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
jayant.tripathi wrote on Tue, 08 October 2013 11:26
Hi
Thanks for your responses.

I have tried TO_DATE (to_char(max_date,'fm000000000000'), 'yyyymmddhh24mi') + 1" but it did not work.

ANy other suggestion. Writing whole logic again is not possble at the moment by taking dates as DATE. Tat table is being used by several fucntionalities. SO how to cope up with this NUMBER thing?

Thanks.


Considering pretty much all your date logic in the query is suspect I don't think you have any choice but to rewrite the query. If you can't change how the data is stored then use a with clause to convert the number to a date once at the start and then apply all logic to the resulting dates.
Re: ORA-01840: input value not long enough fro date format. [message #597802 is a reply to message #597690] Tue, 08 October 2013 09:07 Go to previous messageGo to next message
jayant.tripathi
Messages: 8
Registered: October 2013
Junior Member
i have changed the to_date(d.maxdate +1, 'yyyymmddhhmiss') to to_date(d.maxdate, 'yyyymmddhhmiss') and it worked.

It is now confirmed that problem is with maxdate+1 for last day of any month. To_char is also not working. It is giving me Incorrect query error.

Can you suggest me any other option to make it work properly.

Thanks for your help.
Re: ORA-01840: input value not long enough fro date format. [message #597803 is a reply to message #597802] Tue, 08 October 2013 09:10 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>To_char is also not working. It is giving me Incorrect query error.
Problem Exists Between Keyboard And Chair.

Why do you expect us to tell you what you are doing wrong when you refuse to actually show us what exactly your are doing???????????????

is COPY & PASTE broken for you?
icon13.gif  Re: ORA-01840: input value not long enough fro date format. [message #597806 is a reply to message #597802] Tue, 08 October 2013 09:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that you now use "yyyymmddhhmiss" when you said "yyyymmddhhmi".
Maybe this why what I posted "did not work".


Re: ORA-01840: input value not long enough fro date format. [message #597808 is a reply to message #597806] Tue, 08 October 2013 09:27 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
jayant.tripathi wrote on Tue, 08 October 2013 15:07
i have changed the to_date(d.maxdate +1, 'yyyymmddhhmiss') to to_date(d.maxdate, 'yyyymmddhhmiss') and it worked.

I hope you changed it to to_date(d.maxdate, 'yyyymmddhhmiss') + 1.
Though that means the original was adding a second and the new version is adding a day.
Plus your query above uses 'yyyymmddhhmi' (no ss) so that's really confused things.

jayant.tripathi wrote on Tue, 08 October 2013 15:07

To_char is also not working.

you have several, which one?

jayant.tripathi wrote on Tue, 08 October 2013 15:07

It is giving me Incorrect query error.


that's not a valid oracle error message and tells us nothing useful.

We are not stood over your shoulder, we can't see what code you're running, we don't have access to your tables or data and we don't actually know what the query is supposed to do. We only know what you tell us. So if you want help fixing it you need to supply more information.
Re: ORA-01840: input value not long enough fro date format. [message #597823 is a reply to message #597808] Tue, 08 October 2013 10:04 Go to previous messageGo to next message
jayant.tripathi
Messages: 8
Registered: October 2013
Junior Member
My Thanks to Michel for helping me. It worked. Smile

icon4.gif  Re: ORA-01840: input value not long enough fro date format. [message #597829 is a reply to message #597823] Tue, 08 October 2013 10:57 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Also thanks other people they have much time to try to help you in many different ways that would be useful for you in your professional life.

Previous Topic: SQL Top N Analysis
Next Topic: Dynamic SQL
Goto Forum:
  


Current Time: Fri Apr 26 04:00:33 CDT 2024