SQL将日期/时间减去一个值

时间:2011-10-18 22:20:16

标签: sql db2

我有几栏:

|  Start             |   END              |  Duration
2008-06-28 18.03.55 | 2008-10-06 01.33.55 |  End - Start

我的代码如下所示:

SELECT theDelivery,
(char(theDelivery) || ' ' || char(current time)) AS Begin,
(char(theDelivery + 100 DAYS) || ' ' || char(current time + 7 HOURS + 30 MINUTES)) AS End

这是我在做减法的想法......但是错了。我知道你不能减去别名'任何想法? ((char(当前时间+7小时+30分钟) - (char(theDelivery + 100 DAYS)))


所以我发现我对char的问题一切都错了。我想出了这个,这正是我想要的:

SELECT theDelivery,
TIMESTAMP(theDelivery, current time) AS Begin
,
TIMESTAMP((theDelivery + 100 DAYS), (current time + 7 HOURS + 30 MINUTES)) AS End,
TIMESTAMP((theDelivery + 100 DAYS), (current time + 7 HOURS + 30 MINUTES)) - TIMESTAMP(theDelivery, current time) AS MyDur

1 个答案:

答案 0 :(得分:1)

如果你这样做:

<(>(char(当前时间+7小时+30分钟) - (char(交付+ 100天)))

你正在减去字符。你可以尝试:

date( 
  to_date(
      char(current time + 7 HOURS + 30 MINUTES),
      'YYYY-MM-DD HH.MI.SS'
  ) 
) -
date(
   to_date(
      char(theDelivery + 100 DAYS),
      'YYYY-MM-DD HH.MI.SS'
   )
)

或者在一条线上:

date( to_date( char(current time + 7 HOURS + 30 MINUTES),'YYYY-MM-DD HH.MI.SS' ) ) - date( to_date(char(theDelivery + 100 DAYS),'YYYY-MM-DD HH.MI.SS') )