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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 calculating days from datetime

Author  Topic 

waxdart23
Starting Member

33 Posts

Posted - 2005-06-01 : 11:13:44
If I have two columns containing dates in datetime format
eg '2004-09-19 17:30:00.000' & '2004-09-24 14:30:00.000'

How do I calculate the number of days between the two dates (in the case above 5 days) regardles of the time and store it as an int?

Thanks
P

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2005-06-01 : 11:24:55
Lookup the DATEDIFF function

Mark
Go to Top of Page

maninder
Posting Yak Master

100 Posts

Posted - 2005-06-01 : 11:27:32
Here is What we used to Calculate the number of days between 2 Dates.

Create PROCEDURE Busdays @from_dt datetime,@to_dt datetime ,@wkends int output
AS
DECLARE
@days int,
@dp int ,
@cnt int
set @wkends=0
set @cnt=0
set @days=datediff(d,@from_dt,@to_dt)
while @cnt <=@days
begin
set @dp=datepart(dw,dateadd(day,@cnt,@from_dt))
if @dp=1 or @dp=7
set @wkends=@wkends+1
set @cnt=@cnt+1
End


Maninder
Go to Top of Page

waxdart23
Starting Member

33 Posts

Posted - 2005-06-01 : 11:48:59
DATEDIFF works great thanks for the pointer

Thanks
P
Go to Top of Page
   

- Advertisement -