SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 DateAdd from Max Date Value Column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

24 Posts

Posted - 01/08/2014 :  13:48:57  Show Profile  Reply with Quote
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?

In (Som, Ni, Yak)

17684 Posts

Posted - 01/08/2014 :  21:16:31  Show Profile  Reply with Quote

@StartDate = DATEADD(YEAR, DATEDIFF(YEAR, 0, @EndDate), 0)

Time is always against us

Go to Top of Page

Premature Yak Congratulator

22847 Posts

Posted - 01/09/2014 :  07:12:44  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
More querying on datetime columns http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000