简体   繁体   English

SUBSTRING和SUBSTR函数(SQL)有什么区别?

[英]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的SUBSTRING?

SUBSTR is the function from Oracle SUBSTR是Oracle的函数

SUBSTRING is the function from MySql SUBSTRING是MySql的函数

depends on DB which ur using 取决于您使用的数据库

EDIT: 编辑:

try to edit your java code like below 尝试如下编辑您的Java代码

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 ... Oracle的SUBSTR()的正确语法为...

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. SUBSTR()不是布尔函数。

Whereas SUBSTRING() is not an oracle function at all. 而SUBSTRING()根本不是oracle函数。 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. 子字符串是sql标准ISE:IEC 9075:1992中定义的sql操作。

substr is an old syntax used by oracle. substr是oracle使用的旧语法。 This wrong syntax is completely inconsistent with sql usage of real english words, never abbreviations. 这种错误的语法与真实英语单词的sql用法完全不一致,从不缩写。

Oracle still does not support the standard syntax. Oracle仍然不支持标准语法。

Did anyone wrote a hack in oracle to support the standard syntax ? 是否有人在oracle中编写了用于支持标准语法的hack?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM