简体   繁体   中英

ORA-01858: a non-numeric character was found where a numeric was expected (timestamp, decode)

I'm getting this error ORA-01858: a non-numeric character was found where a numeric was expected . If I remove the second line in my code I don't get the error, so I'm wondering if the issue has something to do with the DECODE or datatype of the TIME_P variable, which is timestamp.

TRUNC(TIME_P) >= TO_DATE('2009-01-01', 'YYYY-MM-DD') AND
TIME_P <= DECODE('2010-01-01', NOW, SYSDATE, TO_DATE('YYYY-MM-DD'))

Could I suggest something like this instead?

TIME_P >= DATE '2009-01-01' AND
TIME_P < (CASE WHEN NOW = '2010-01-01' THEN DATE '2010-01-01' ELSE SYSDATE END)

This uses date literals, case instead of decode() ( case is standard SQL). The initial trunc() is redundant, as well.

The second line doesn't really make sense to me. If you just want dates that are not in the future, then:

TIME_P >= DATE '2009-01-01' AND
TIME_P < sysdate

Or if you want to pass in a parameter and use that instead of sysdate:

TIME_P >= DATE '2009-01-01' AND
TIME_P < COALESCE(:parameter, sysdate)

This is what you are asking for I believe:

DECODE(YOUR_FIRST_PARAMETER_HER, '2010-01-01', SYSDATE, TO_DATE(TRUNC(TIME_P), 'DD-MON-YYYY'))

AS you have mentioned "First arg is optional param" so I have entered the "YOUR_FIRST_PARAMETER_HER" expresion just to note to you that you will put it there.

Your error was occurring because of the format you have entered ('YYYY-MM-DD') because the correct format is 'DD-MON-YYYY' Also, before converting you will need to trunc that timestamp value...

Here you can see the errors that will occur if you do not use the right format and if you do not use trunc: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=83cca74815a323bc72283adeb7396617

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