April 11, 2006 at 8:36 am
does anyone know of (or can anyone create off the top of their head) a formula for finding the date of the previous august, given a date?
i can use a CASE statement i.e.
CASE WHEN DATEPART(MONTH, @Date) >= 8 THEN CAST(DATEPART(YEAR, @Date) AS CHAR(4)) + '-08-01' ELSE CAST(DATEPART(YEAR, @Date) - 1 AS CHAR(4)) + '-08-01' END
but i figured maybe someone would have something a bit nicer.
-- Stephen Cook
April 11, 2006 at 8:46 am
This any better?
select
case when month(@date) > 8 then convert(varchar,year(@date))
else convert(varchar,year(@date) - 1)
end
+ '-08-01'
April 11, 2006 at 6:11 pm
declare @Date datetime
select @Date = getdate()
-- @m is a number of a month you take care about. You need August - so it's 8.
-- When you need another month just change value supplied to this parameter
select dateadd(YY, case when Month(@date) < @m then -1 else 0 end + Year(@Date) - Year(0), dateadd(mm, @M-month(0), 0))
_____________
Code for TallyGenerator
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy