提交 ba32fd21 authored 作者: Thomas Mueller's avatar Thomas Mueller

MySQL compatibility: SUBSTR with a negative start index now works like MySQL.

上级 ced610b7
......@@ -3096,10 +3096,12 @@ CALL UTF8TOSTRING(STRINGTOUTF8('This is a test'))
{ SUBSTRING | SUBSTR } ( string, startInt [, lengthInt ] )
","
Returns a substring of a string starting at a position.
If the start index is negative, then the start index is relative to the end of the string.
The length is optional.
Also supported is: ""SUBSTRING(string FROM start [FOR length])"".
","
SUBSTR(NAME, 1)
CALL SUBSTR('[Hello]', 2, 5);
CALL SUBSTR('Hello World', -5);
"
"Functions (String)","UTF8TOSTRING","
......
......@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>When enabling autocommit, the transaction is now committed (as required by the JDBC API).
<ul><li>MySQL compatibility: SUBSTR with a negative start index now works like MySQL.
</li><li>When enabling autocommit, the transaction is now committed (as required by the JDBC API).
</li><li>The shell script <code>h2.sh</code> did not work with spaces in the path.
It also works now with quoted spaces in the argument list.
Thanks a lot to Shimizu Fumiyuki for the patch!
......
......@@ -1049,8 +1049,12 @@ public class Function extends Expression implements FunctionCall {
case SUBSTR:
case SUBSTRING: {
String s = v0.getString();
int offset = v1.getInt();
if (offset < 0) {
offset = s.length() + offset + 1;
}
int length = v2 == null ? s.length() : v2.getInt();
result = ValueString.get(substring(s, v1.getInt(), length));
result = ValueString.get(substring(s, offset, length));
break;
}
case POSITION:
......
select substr('[Hello]', 2, 5);
> Hello;
select substr('Hello World', -5);
> World;
create table test(id null);
drop table test;
select select decode(null, null, 'a');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论