繁体   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