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)
 DateAdd from Max Date Value Column

Author  Topic 

rankone
Starting Member

24 Posts

Posted - 2014-01-08 : 13:48:57
Hey Everyone,

I am trying to generate a @StartDate and @EndDate. The @StartDate would be the 1st of January and the year is dependent on the @EndDate. The @EndDate would be
select max(DateValue) from dbo.testtable

The column DateValue in the testtable contains dates such as 2013-12-09, 2013-12-15.

What I am trying to accomplish is if the @EndDate is populated by select max(DateValue) from dbo.testtable returns '2013-12-15' as the max(DateValue). I want the @StartDate to '2013-01-01' because the year of the EndDate is 2013.

So something along the lines of
@StartDate = DateAdd(yy,?,@EndDate)


Any suggestions?
Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-01-08 : 21:16:31
[code]
@StartDate = DATEADD(YEAR, DATEDIFF(YEAR, 0, @EndDate), 0)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-01-09 : 07:12:44
More querying on datetime columns http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

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

- Advertisement -