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
 Calculating a difference between two columns

Author  Topic 

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2014-02-12 : 07:37:11
HI Guys

I need some help

Aim - To calculate how many months are between the current date minus the First_Post_Date

For example
The First_Post_Date is displayed as follows following “25/07/2012”

Current date is 12-02-2014

The difference between the two dates is approx 20months

To make the calculation easier, it might be easier to default the day of First_Post_Date to 01

and do the same with the currentdate

So it would be 01/07/12 and 01/02/14

Looking forward to your help

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-12 : 07:38:31
DATEDIFF(mm,First_Post_Date,GETDATE())

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2014-02-12 : 07:42:10
hi Visakh16

I get the following error msg

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-12 : 08:35:29
quote:
Originally posted by masond

hi Visakh16

I get the following error msg

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.


whats the data type of First_Post_Date?
anyways try this too

DATEDIFF(mm,CONVERT(datetime,First_Post_Date,103),GETDATE())


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -