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 2012 Forums
 Transact-SQL (2012)
 get last year

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2015-03-11 : 06:33:12
hello,
I would ike to get this query to work:
select...
from...
where...
and datediff(m, MyDate, convert(date, '31 Dec ' + dateadd(yy, -1, YEAR(tbl1.Date)))) >=12

note that tbl1.Date has a value of '03/07/2015'
I basically am trying to make sure the returned data is 12 month or more.
Any suggestions please?
Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2015-03-11 : 07:36:19
Is this?

Where date>=dateadd(month,-12,getdate())

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunder.bugatha
Yak Posting Veteran

66 Posts

Posted - 2015-03-11 : 10:00:13
It should be something like below :

convert(date,'31 Dec' + convert(varchar(10),year(DATEADD(y,-1,tbl1.Date))))

Hema Sunder
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2015-03-12 : 06:13:04
quote:
Originally posted by sunder.bugatha

It should be something like below :

convert(date,'31 Dec' + convert(varchar(10),year(DATEADD(y,-1,tbl1.Date))))

Hema Sunder


You are making it as ambiguous date format. Always use unambiguous date format as shown here http://beyondrelational.com/modules/2/blogs/70/posts/10898/understanding-datetime-column-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -