Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DATETIME calculations 2

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
I have a smalldatetime column in a database that I want to calculate the number of days left to that date from the current date.

Are there any methods to do this? Can anyone help me with this? Thanks.
 
Use datediff:

Code:
declare @a table (dte smalldatetime)
insert into @a
select '2007-01-01' union
select '1900-01-01' union
select '2008-01-01'

select dte, datediff(dd, dte, getdate()) diff from @a

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
SWitch getdate() and dte if you want a positive number of days for dates in dte that are in the future.
Of course if some of the dates might have already happened and you don't want to return a negative number, you will want a where clause

Code:
where getdate()<dte

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top