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 2005 Forums
 Transact-SQL (2005)
 help with date manipulation

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2008-11-14 : 10:40:30
Hi,

I have a simple query I am kinda stuck on. I have a query in which I select a value and compare it with getDate(). I want to compare how many months ago it was.

For example, (todays date is 11/14/2008)

the date 11/11/2008, compared with getDate will return 0
the date 10/31/2008, compared with getDate will return 1
the date 10/01/2008, compared with getDate will return 1
the date 09/15/2008, compared with getDate will return 2


Can anyone lend a hand on this one ?

Thanks once again!
mike123



jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-11-14 : 10:59:33
select datediff(month,'09/15/2008',getdate())

Jim
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2008-11-14 : 11:05:14
oops, thought I was trying something more difficult.. nonetheless very much appreciated!

thx :)
mike123
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 11:07:38
quote:
Originally posted by mike123

oops, thought I was trying something more difficult.. nonetheless very much appreciated!

thx :)
mike123


is this used in where condition or just in select?
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2008-11-14 : 11:13:16
Hey Visakh16,

just in a select statement like so

SELECT datediff(month,ISNULL(MIN(clickDate),getDate()),getdate()) as myFirst_clickSent_MonthsAgo


thx! :)
mike123
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-11-14 : 11:15:00
http://www.sqlteam.com/article/datediff-function-demystified


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

- Advertisement -