【问题标题】:last_day function not working in DB2last_day 函数在 DB2 中不起作用
【发布时间】:2014-06-10 18:26:53
【问题描述】:

数据库:DB2 v9.5.301.436

要求:我需要找出一个月中的天数。

代码:

select day(last_day(created)) 
from tablename

错误:

[Error Code: -440, SQL State: 42884]  DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=LAST_DAY;FUNCTION, DRIVER=3.57.82. 2) 
[Error Code: -727, SQL State: 56098]  DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-440;42884;LAST_DAY|FUNCTION, DRIVER=3.57.82

我查看了 DB2 文档,其中显示上述功能可用。 http://www-01.ibm.com/support/knowledgecenter/api/content/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_bif_lastday.html

【问题讨论】:

  • 我认为您应该查看您实际拥有的 DB2 版本和平台的手册。

标签: sql db2


【解决方案1】:

以下内容对我来说很好。

select day(last_day(current_timestamp)) 
from SYSIBM.SYSDUMMY1

您确定created 是约会对象吗?

【讨论】:

  • 我也检查了这个,它对我不起作用。 DB2 v9.5.301.436 可能不支持您使用的哪个版本。
  • 创建的是时间戳
【解决方案2】:

下面的代码对我有用。

select day(date(date(created))-(day(date(created)) -1) days + 1 month -1 day)  as maxdate  , created  from customer  ;   

注意:创建的列是时间戳

如果 created 是 Date 可以使用下面的查询。

select day(date(created)-(day(created) -1) days + 1 month -1 day)  as maxdate  , created  from customer  ;  

【讨论】:

    【解决方案3】:

    last_day 在 9.7 中引入。你可以自己滚动:

    create function myfun.last_day(d date) 
    returns date 
    return (d + 1 month) - day(d + 1 month) days
    
    with t(d) as ( 
        values date('2014-01-11') 
        union all 
        select d + 1 month 
        from t 
        where d<'2015-01-01'
    ) 
    select d, myfun.last_day(d) from t
    

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 2016-06-04
      • 1970-01-01
      • 2013-08-07
      • 2017-05-13
      相关资源
      最近更新 更多