Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How To Calculate Dates

Author  Topic 

cnbhold
Starting Member

43 Posts

Posted - 2010-12-01 : 15:55:10
I have a table with a DateStamp Column. How would I calculate the number of days between the date in the column and todays date. Then update the DaysPastDue with number of days that have past.

Sample Table Name: Payments

CustomerID int Primary Key,
FirstName varchar(50),
LastName varchar(50),
DueDate DateTime,
DaysPastDue Int

Angel

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-01 : 16:19:35
Why do you want to store DaysPastDue?
If you are calculating the days today then tomorrow the result will be wrong.
Why not calculating DaysPastDue whenever you need it without storing it?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-01 : 16:30:37
Or use a COMPUTED COLUMN:

create table Payments(
CustomerID int identity(1,1),
FirstName varchar(50),
LastName varchar(50),
DueDate DateTime,
DaysPastDue as datediff(day,DueDate,dateadd(day,datediff(day,0,getdate()),0))
)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-12-01 : 16:45:19
DaysPastDue AS datediff(day, DueDate, getdate())

will suffice



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-01 : 16:47:44
quote:
Originally posted by Peso

DaysPastDue AS datediff(day, DueDate, getdate())

will suffice



N 56°04'39.26"
E 12°55'05.63"



You are right


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

cnbhold
Starting Member

43 Posts

Posted - 2010-12-01 : 16:48:20
That worked. Thanks for your help!

Angel
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-01 : 16:50:20
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -