简体   繁体   中英

what is the difference between SUBSTRING and SUBSTR functions (SQL)?

before I used :

entityManagerFactory.createQuery("select p FROM Pays p where SUBSTRING(p.libeleClient, 0,1)

but when I use this query :

entityManagerFactory.createQuery("select p FROM Pays p where SUBSTR(p.libeleClient, 0,1)

I get an exception :(

who to remplace SUBSTRING by SUBSTR ?

SUBSTR is the function from Oracle

SUBSTRING is the function from MySql

depends on DB which ur using

EDIT:

try to edit your java code like below

String query = "select p FROM Pays p where SUBSTRING(p.libeleClient, 0,1)";


        // from Connection Object (connection)
        DatabaseMetaData meta = connection.getMetaData();
        //If the DB is Oracle 
          if(meta.getDatabaseProductName()).contains("Oracle")) {
              entityManagerFactory.createQuery(query.replace("SUBSTRING", "SUBSTR"));
          }// If the DB not Oracle , any Other like MySql 
          else {
              entityManagerFactory.createQuery(query);
          }

You don't say what exception you get, but I 'm guessing it's a syntax error. The correct syntax for Oracle's SUBSTR() is ...

where SUBSTR(p.libeleClient, 0,1) = 'X'

...(or whatever). That is the first occurence of a single character must equal; some specified value. SUBSTR() is not a boolean function.

Whereas SUBSTRING() is not an oracle function at all. Either you've borrowed the syntax from some other database, or you're using a bespoke function without realising it.


"I tried your suggestion but it does not work"

Do you get an error? Or do you mean it doesn't return any records? Because I have given a perfectly valid usage, as defined in the documentation . But you haven't given any examples of your data, so it's almost impossible for me to provide a solution which will return rows from your database.

substring is the sql operation defined in the sql standard ISE:IEC 9075:1992.

substr is an old syntax used by oracle. This wrong syntax is completely inconsistent with sql usage of real english words, never abbreviations.

Oracle still does not support the standard syntax.

Did anyone wrote a hack in oracle to support the standard syntax ?

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