简体   繁体   中英

invalid number oracle jdbc

I'm trying to execute the following query:

String query =  "select NUMERO_CHAUFFEUR, avg(DISTANCE) as DISTANCE " +
                "from " +
                "(select NUMERO_CHAUFFEUR, " +
                "6387.7 * ACOS((sin(LATITUDE_DEPART / 57.29577951) * SIN(LATITUDE_ARRIVEE / 57.29577951)) + " +
                "(COS(LATITUDE_DEPART / 57.29577951) * COS(LATITUDE_ARRIVEE / 57.29577951) * " +
                "COS(LONGITUDE_ARRIVEE / 57.29577951 - LONGITUDE_DEPART / 57.29577951))) as DISTANCE " +
                "from " +
                "(select l.NUMERO_CHAUFFEUR, " +
                "regexp_substr(d1.COORDONNEES, '^[^,]+') as LATITUDE_DEPART, " +
                "trim(leading ',' FROM regexp_substr(d1.COORDONNEES, ',.*$')) AS LONGITUDE_DEPART, " +
                "regexp_substr(d2.COORDONNEES, '^[^,]+') as LATITUDE_ARRIVEE, " +
                "trim(leading ',' FROM regexp_substr(d2.COORDONNEES, ',.*$')) AS LONGITUDE_ARRIVEE " +
                "from LIVRAISONS l " +
                "inner join DEPOTS d1 on(l.NUMERO_DEPOT_DEPART = d1.NUMERO_DEPOT) " +
                "inner join DEPOTS d2 on(l.NUMERO_DEPOT_ARRIVE = d2.NUMERO_DEPOT) " +
                ")) " +
                "group by (NUMERO_CHAUFFEUR)";

But a java.sql.SQLException: ORA-01722: invalid number is being thrown. Does anyone know why? Because if I execute the query directly in sql using sqlplus it works fine.

Result of execution of the query using sqlplus:

NUMERO_CHAUFFEUR AVG(DISTANCE)
---------------- -------------
           1    507.064894
           2    703.326572
           5    846.966137
           4    511.914202

I've tried the following but the error still persists:

String query =  "select NUMERO_CHAUFFEUR, avg(DISTANCE) as DISTANCE " +
                "from " +
                "(select NUMERO_CHAUFFEUR, " +
                "to_number('6387.7') * ACOS((sin(LATITUDE_DEPART / to_number('57.29577951')) * SIN(LATITUDE_ARRIVEE / to_number('57.29577951'))) + " +
                "(COS(LATITUDE_DEPART / to_number('57.29577951')) * COS(LATITUDE_ARRIVEE / to_number('57.29577951')) * " +
                "COS(LONGITUDE_ARRIVEE / to_number('57.29577951') - LONGITUDE_DEPART / to_number('57.29577951')))) as DISTANCE " +
                "from " +
                "(select l.NUMERO_CHAUFFEUR, " +
                "to_number(regexp_substr(d1.COORDONNEES, '^[^,]+')) as LATITUDE_DEPART, " +
                "to_number(trim(leading ',' FROM to_number(regexp_substr(d1.COORDONNEES, ',.*$')))) AS LONGITUDE_DEPART, " +
                "to_number(regexp_substr(d2.COORDONNEES, '^[^,]+')) as LATITUDE_ARRIVEE, " +
                "to_number(trim(leading ',' FROM to_number(regexp_substr(d2.COORDONNEES, ',.*$')))) AS LONGITUDE_ARRIVEE " +
                "from LIVRAISONS l " +
                "inner join DEPOTS d1 on(l.NUMERO_DEPOT_DEPART = d1.NUMERO_DEPOT) " +
                "inner join DEPOTS d2 on(l.NUMERO_DEPOT_ARRIVE = d2.NUMERO_DEPOT) " +
                ")) " +
                "group by (NUMERO_CHAUFFEUR)";

An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a number. regexp_substr would return string and you called it as LATITUDE_DEPART, then you do mathematics with that value which is not correct. you should first convert it as number. this might help you:

TO_NUMBER(regexp_substr(d1.COORDONNEES, '^[^,]')) LATITUDE_DEPART

Do this for all same parts.

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