简体   繁体   中英

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 . 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.

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.

Any pointers would be greatly appreciated.

SQL would look like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM