简体   繁体   English

SQL-计算天数

[英]SQL - Calculate number of days

I am trying to calculate the number of days customers pay invoices either early or late. 我正在尝试计算客户提前或延迟支付发票的天数。

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

This is great for customers with a Date of Invoice term. 这对于具有“发票日期”条款的客户来说非常有用 The problem is for customers with for instance 60 days end of month . 问题是例如月底有60天的客户。 I am struggling to come up with a way using the InvoiceDate and using two other fields InvDayOfMonth = '31' and InvMonths = '2' to calculate the number of days. 我正在努力想出一种使用InvoiceDate的方法,并使用另外两个字段InvDayOfMonth = '31' InvMonths = '2' InvDayOfMonth = '31'InvMonths = '2'来计算天数。

In laymans terms I need to calculate the number of days from the InvoiceDate until 31 days, add this to the then multiplied InvMonth by 31. 用外行术语来说,我需要计算从InvoiceDate到31天的天数,将其加到乘以31的InvMonth

Any pointers would be greatly appreciated. 任何指针将不胜感激。

SQL would look like this 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')))

Due to Your Comment the answer is simple then 由于您的评论,答案很简单

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