January 8, 2014 at 11:51 am
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
January 8, 2014 at 11:55 am
Something like this?
declare @EndDate datetime = '2013-12-15'
select dateadd(yy, datediff(yy, 0, @EndDate), 0) -- Beginning of this year
This came from Lynn's blog post with lots of common datetime routines. http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 8, 2014 at 1:42 pm
Thanks Sean, that worked!
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply