簡體   English   中英

SQL-計算天數

[英]SQL - Calculate number of days

我正在嘗試計算客戶提前或延遲支付發票的天數。

select 
    a.Invoice, 
    datediff(day,InvoiceDate,JournalDate) as Days, 
    c.DueDays, 
    c.Description as Terms  

from 
    ArInvoice a 
    Inner Join ArInvoicePay b on a.Invoice = b.Invoice 
    Inner Join TblArTerms c on a.TermsCode = c.TermsCode

這對於具有“發票日期”條款的客戶來說非常有用 問題是例如月底有60天的客戶。 我正在努力想出一種使用InvoiceDate的方法,並使用另外兩個字段InvDayOfMonth = '31' InvMonths = '2' InvDayOfMonth = '31'InvMonths = '2'來計算天數。

用外行術語來說,我需要計算從InvoiceDate到31天的天數,將其加到乘以31的InvMonth

任何指針將不勝感激。

SQL看起來像這樣

SELECT 
dateadd(day,-1,
dateadd(month, 2, --Your variable here
--replace getdate with Invoice date, and figure out the 0+month part
convert(DATE, cast(Year(getdate()) as NVARCHAR) + '0'+cast(Month(getdate()) as NVARCHAR) + '01')))

由於您的評論,答案很簡單

SELECT 
dateadd(day,60,InvoiceDate) 
--replace 60 with the number You've already calculated

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM