LightDB - datediff 函数增强[mysql兼容]

LightDB在 23.4 版本对原先支持的mysql的datediff函数进行了增强,由原先只支持如下函数匹配:

DATEDIFF(expr1 timestamp,expr2 timestamp) RETURN integer
DATEDIFF(expr1 text,expr2 text) RETURN integer

扩展到支持如下函数匹配:

DATEDIFF(expr1 timestamp,expr2 timestamp) RETURN integer
DATEDIFF(expr1 text,expr2 text) RETURN integer
DATEDIFF(expr1 timestamptz,expr2 timestamptz) RETURN integer
DATEDIFF(expr1 timestamptz,expr2 timestamp) RETURN integer
DATEDIFF(expr1 timestamp,expr2 timestamptz) RETURN integer
DATEDIFF(expr1 date, expr2 date) RETURN integer

datediff 简介

datediff 用于计算两个日期的差值,下面是mysql中对其的介绍:

DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

注意点

  • 在LightDB 中不支持time 类型, mysql支持(在mysql中time类型可以大于24小时,因此差值可以不为0)
  • LightDB 对于错误格式的日期会报错,mysql 会截断,并warning

示例

lightdb:

lightdb@test_m=# SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
 datediff 
----------
        1
(1 row)

lightdb@test_m=# SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
 datediff 
----------
      -31
(1 row)

lightdb@test_m=# SELECT DATEDIFF(cast('2007-12-31 23:59:59' as datetime),cast('2007-12-30' as datetime));
 datediff 
----------
        1
(1 row)

lightdb@test_m=# SELECT DATEDIFF(cast('2007-12-31 23:59:59' as date),cast('2007-12-30' as date));
 datediff 
----------
        1
(1 row)

lightdb@test_m=# SELECT DATEDIFF(cast('10:00:00' as time),cast('11:00:00' as time));
ERROR:  function datediff(time without time zone, time without time zone) does not exist
LINE 1: SELECT DATEDIFF(cast('10:00:00' as time),cast('11:00:00' as ...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
lightdb@test_m=# 
lightdb@test_m=# SELECT DATEDIFF(20121212,20121215);
 datediff 
----------
       -3
(1 row)

lightdb@test_m=# SELECT DATEDIFF(20121212.5,20121215.1);
ERROR:  invalid input syntax for type date: "20121212.5"
CONTEXT:  SQL function "datediff" statement 1
lightdb@test_m=# 

mysql:

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
+----------------------------------------------+
| DATEDIFF('2007-12-31 23:59:59','2007-12-30') |
+----------------------------------------------+
|                                            1 |
+----------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
+----------------------------------------------+
| DATEDIFF('2010-11-30 23:59:59','2010-12-31') |
+----------------------------------------------+
|                                          -31 |
+----------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(cast('2007-12-31 23:59:59' as datetime),cast('2007-12-30' as datetime));
+----------------------------------------------------------------------------------+
| DATEDIFF(cast('2007-12-31 23:59:59' as datetime),cast('2007-12-30' as datetime)) |
+----------------------------------------------------------------------------------+
|                                                                                1 |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(cast('2007-12-31 23:59:59' as date),cast('2007-12-30' as date));
+--------------------------------------------------------------------------+
| DATEDIFF(cast('2007-12-31 23:59:59' as date),cast('2007-12-30' as date)) |
+--------------------------------------------------------------------------+
|                                                                        1 |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(cast('10:00:00' as time),cast('11:00:00' as time));
+-------------------------------------------------------------+
| DATEDIFF(cast('10:00:00' as time),cast('11:00:00' as time)) |
+-------------------------------------------------------------+
|                                                           0 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(cast('100:00:00' as time),cast('11:00:00' as time));
+--------------------------------------------------------------+
| DATEDIFF(cast('100:00:00' as time),cast('11:00:00' as time)) |
+--------------------------------------------------------------+
|                                                            4 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(20121212,20121215);
+-----------------------------+
| DATEDIFF(20121212,20121215) |
+-----------------------------+
|                          -3 |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(20121212.5,20121215.1);
+---------------------------------+
| DATEDIFF(20121212.5,20121215.1) |
+---------------------------------+
|                              -3 |
+---------------------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect date value: '20121212.5' |
| Warning | 1292 | Truncated incorrect date value: '20121215.1' |
+---------+------+----------------------------------------------+
2 rows in set (0.00 sec)

mysql> 


你可能感兴趣的:(lightdb,mysql,lightdb,datediff)