Db2

在 DB2 中添加日期(帶小數的字元列)

  • October 14, 2021

我有一個名為 INVOICE_ET 的表,其中包含以下定義的列

INVDATE CHARACTER 10

從 INVOICE_ET 中選擇 INVDATE 的結果如下

INVDATE
2021-07-31

在另一個表 INVOICE_DY 中,我有一個名為 INV_DAYS 的列,其定義如下

INV_DAYS  DECIMAL 3 

從 INVOICE_DY 中選擇 INV_DAYS 的結果如下

INV_DAYS 
88 

現在我的問題是,我正在嘗試使用 INVOICE_ET.INVDATE 欄位添加 INVOICE_DY.INV_DAYS 欄位,但是字元轉換不起作用,請建議如何添加這兩列。我試圖得到類似下面的東西

Select a.INVDATE, b.INV_DAYS, (a.INVDATE + b.INV_DAYS) as DUE_DATE 
from INVOICE_ET a 
join INVOICE_DY b 
On a.INVNUM = b.INVNUM //(edited this portion, because join is based on Invoice numbers)
Result should be 
INVDATE       INV_DAYS       DUE_DATE 
2021-07-31    88             2021-10-27 

歡迎任何建議/想法。

非常感謝 Akina 和 ypercubeᵀᴹ 整理出這個。

我已經以以下方式修改了查詢並且它有效。

SELECT INVOICE_ET.INVDATE,
      INVOICE_DY.INV_DAYS,
     (CAST(INVOICE_ET.INVDATE AS date) + INVOICE_DY.INV_DAYS DAYS) AS DUE_DATE 
FROM INVOICE_ET
CROSS JOIN INVOICE_DY;

有關更多詳細資訊,請參閱此小提琴

SELECT INVOICE_ET.INVDATE,
      INVOICE_DY.INV_DAYS,
      ADD_DAYS(DATE(INVOICE_ET.INVDATE), INVOICE_DY.INV_DAYS) AS DUE_DATE 
FROM INVOICE_ET
CROSS JOIN INVOICE_DY;

https://dbfiddle.uk/?rdbms=db2_11.1&fiddle=de4bc1d961771ab8826a27eda0855df7

引用自:https://dba.stackexchange.com/questions/300993