LightDB - 支持 last_day 函数[mysql兼容]

从 23.4 开始 LightDB 支持 mysql 的 last_day 函数。
目前LightDB 实现last_day的与mysql 并不完成相同,会在示例中列举出不同点, 主要是以下几点:

  • 对于错误日期mysql返回null,lightdb 是直接报错
  • 对于一些特殊日期,lightdb 不支持
  • LightDB 只支持date 和text 类型(datetime 目前不支持)

last_day 简介

last_day 用于获取指定日期所在月份的最后一天,下面是mysql中的介绍:

Takes a date or datetime value and returns the corresponding value for the last day of the month. Returns NULL if the argument is invalid or NULL.

示例

lightdb@test_m=# select last_day('2000-02-05') as f1, last_day('2002-12-31') as f2,lightdb@test_m-# last_day('2003-04-01') as f3,
lightdb@test_m-# last_day('2001-01-01 01:01:01') as f4, last_day(NULL) as f5,
lightdb@test_m-# last_day('2001-02-12') as f6;
     f1     |     f2     |     f3     |     f4     | f5 |     f6     
------------+------------+------------+------------+----+------------
 2000-02-29 | 2002-12-31 | 2003-04-30 | 2001-01-31 |    | 2001-02-28
(1 row)

lightdb@test_m=# select last_day(cast('20220101' as date));
  last_day  
------------
 2022-01-31
(1 row)

报错及与mysql不同点:

lightdb
lightdb@test_m=# select last_day(cast('20220501' as datetime));
ERROR:  function last_day(timestamp without time zone) does not exist
LINE 1: select last_day(cast('20220501' as datetime));
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
lightdb@test_m=# select last_day (cast('2003-02-29' as date));
ERROR:  date/time field value out of range: "2003-02-29"
LINE 1: select last_day (cast('2003-02-29' as date));
                              ^
lightdb@test_m=# select last_day('2005-00-00');
ERROR:  date/time field value out of range: "2005-00-00"
HINT:  Perhaps you need a different "datestyle" setting.
CONTEXT:  SQL function "last_day" statement 1
lightdb@test_m=# select last_day('2005-01-00'); -- 2005-01-31
ERROR:  date/time field value out of range: "2005-01-00"
HINT:  Perhaps you need a different "datestyle" setting.
CONTEXT:  SQL function "last_day" statement 1
lightdb@test_m=# select last_day(0501);
ERROR:  invalid input syntax for type date: "501"
CONTEXT:  SQL function "last_day" statement 1
lightdb@test_m=# select last_day(20220501.1); 
ERROR:  invalid input syntax for type date: "20220501.1"
CONTEXT:  SQL function "last_day" statement 1
lightdb@test_m=# 

mysql
mysql> select last_day(cast('20220501' as datetime));
+----------------------------------------+
| last_day(cast('20220501' as datetime)) |
+----------------------------------------+
| 2022-05-31                             |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select last_day (cast('2003-02-29' as date));
+---------------------------------------+
| last_day (cast('2003-02-29' as date)) |
+---------------------------------------+
| NULL                                  |
+---------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1292 | Incorrect datetime value: '2003-02-29' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

mysql>  select last_day('2005-00-00');
+------------------------+
| last_day('2005-00-00') |
+------------------------+
| NULL                   |
+------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select last_day('2005-01-00'); 
+------------------------+
| last_day('2005-01-00') |
+------------------------+
| 2005-01-31             |
+------------------------+
1 row in set (0.00 sec)

mysql> select last_day(0501);
+----------------+
| last_day(0501) |
+----------------+
| 2000-05-31     |
+----------------+
1 row in set (0.01 sec)

mysql> select last_day(20220501.1);
+----------------------+
| last_day(20220501.1) |
+----------------------+
| 2022-05-31           |
+----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect date value: '20220501.1' |
+---------+------+----------------------------------------------+
1 row in set (0.00 sec)

mysql> 

相关推荐

  1. LightDB - 支持 last_day 函数[mysql兼容]

    2023-12-06 11:16:05       57 阅读
  2. LightDB - 支持substring_index 函数[mysql兼容]

    2023-12-06 11:16:05       52 阅读
  3. LightDB - 支持 curdate, current_date 函数[mysql兼容]

    2023-12-06 11:16:05       60 阅读
  4. LightDB - datediff 函数增强[mysql兼容]

    2023-12-06 11:16:05       53 阅读
  5. LightDB支持 no_expand hint【24.1】【oracle兼容

    2023-12-06 11:16:05       39 阅读
  6. LightDB to_char 三入参函数支持

    2023-12-06 11:16:05       56 阅读
  7. LightDB 24.1 UNION支持null类型匹配

    2023-12-06 11:16:05       52 阅读
  8. LightDB24.1 XMLELEMENT支持省略NAME关键字

    2023-12-06 11:16:05       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 11:16:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 11:16:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 11:16:05       82 阅读
  4. Python语言-面向对象

    2023-12-06 11:16:05       91 阅读

热门阅读

  1. NLP中几个简单的,字符串相似度计算方法

    2023-12-06 11:16:05       54 阅读
  2. AI:大语言模型LLM

    2023-12-06 11:16:05       60 阅读
  3. Pytest 的小例子

    2023-12-06 11:16:05       57 阅读
  4. css基础

    2023-12-06 11:16:05       58 阅读
  5. 什么是供应链金融分账系统?

    2023-12-06 11:16:05       59 阅读
  6. vue2和vue3的区别

    2023-12-06 11:16:05       55 阅读
  7. oracle给用户授权查询权限

    2023-12-06 11:16:05       60 阅读
  8. MISRA C++ 2008 标准解析

    2023-12-06 11:16:05       50 阅读