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
 Dates

Author  Topic 

Petronas
Posting Yak Master

134 Posts

Posted - 2012-04-17 : 15:56:15
Hi All,

I have two dates - '200501'(datein - comes from the database and is stored as a varchar) and '201202'( dateout -comes form the databse and is stored as a datetime) in the 'yymm'format. I need to get the datediff of months between them.

Select datediff(month,'200501', '201202')
does not work .

I am using SQL Server 2008.

Thanks in advance,
Petronas

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-17 : 15:58:29
You need to convert them to proper date format first. Add dd to them first, such as 01.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-17 : 16:12:31
why dont you use proper datatype for dates? by keeping it as varchar you're making it complicated. I hope at least the format is consistent.


select datediff(mm,convert(datetime,datein + '01',112),convert(datetime,dateout + '01',112))
from table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -