SQL - 计算天数

时间:2012-09-03 12:24:33

标签: sql sql-server date

我正在尝试计算客户提前或延迟支付发票的天数。

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'来计算天数。

在外行人的条件下,我需要计算从InvoiceDate到31天的天数,然后将其加到当时乘以InvMonth 31。

任何指针都会非常感激。

1 个答案:

答案 0 :(得分:0)

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