【问题标题】:Turning a custom statement into code that activates将自定义语句转换为激活的代码
【发布时间】:2019-11-04 11:35:28
【问题描述】:

我正在使用日期偏移量,我正在尝试“破解”以下日期函数

now() - interval '12 month'

我想创建一个变量偏移量,而不是使用它,它根据当前月份将月份添加到偏移量中。我创建了以下 SQL:

SELECT now() ||' - interval ' || 
case when to_char(now(),'MON') in('FEB','MAY','AUG','NOV') then 0
     when to_char(now(),'MON') in('MAR','JUN','SEP','DEC') then 1
     when to_char(now(),'MON') in('JAN','APR','JUL','OCT') then 2
ELSE null END + 12 || ' month' TIME_OFFSET;

当我运行它时,我得到以下输出。

TIME_OFFSET  
2019-06-21 11:15:21 - interval 13 month

我正试图让它运行我创建的那个函数来给我以下输出

2018-05-21 11:15:21

我怎样才能让它退回这个?我需要改变什么?

【问题讨论】:

    标签: sql function date netezza


    【解决方案1】:

    刚刚想通了。

    使用以下内容:

    select add_months(now(), (-1 * 
    case when to_char(now(),'MON') in('FEB','MAY','AUG','NOV') then 2
         when to_char(now(),'MON') in('MAR','JUN','SEP','DEC') then 0
         when to_char(now(),'MON') in('JAN','APR','JUL','OCT') then 1
    ELSE null END) -12);
    

    给你:

    ADD_MONTHS
    2018-06-21 16:10:52
    

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 2019-11-25
      • 2019-06-24
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多