轉載自:關於locate函數
由於公司最近將oracle項目移植到db2 中間出現了很多兼容性問題,這裏主要記錄下hibernate中模糊查詢的兼容性問題
例子(hql):from table t where t.name like '%' || t.displayName || '%';
如果遇到這種情況,在oracle裏面是完美兼容,到了db2就出問題了
解決方案:使用locate函數,上面這種情況可以修改成如下
from table t where locate(t.dispalyName,t.name)>0;//這裏等於1就等同於上面sql語句的結果
from table t where locate(t.dispalyName,t.name)=1;,//1就是在name中從第一位開始匹配displayName
from table t where locate(t.dispalyName,t.name)=0;//等於0的結果是排除模糊匹配到的結果,也就是查出匹配不到的所有數據;
from table t where locate(t.dispalyName,t.name)=2;//2就是在t.name中從第二位開始匹配dispalyName;
接下來=3,4,5,6相信大家都理解了.
locate(String param1,String param2,int param3) 從param2的第param3位置開始,返回param1第一次出現的位置,param3爲負數的情況暫時還沒去研究.
hibernate中使用locate會根據你配置的不同方言去解析對應的函數
注意:oracle中locate的參數是反的!!!!
在oracle中要達到上面例子的結果要這樣寫:from table t where locate(t.name,t.dispalyName)=1;
下面是官方實例(可以參考):
LOCATE 函數 [String]
返回一個字符串在另一個字符串中的位置。