March 26, 2008 at 1:12 pm
hello there needs some help in figuring out how to calculate date. here is the scenario:
if the month in today's date is <= June then I would like fdate = todays date +6months
e.g. today's date = 03-26-2008 so fdate = 200809
but if month in today's date >6 then i would like fdate = today's date +11 months - 6 months
so if today's date = 11-01-2008 then fdate = today's date + 11 months - 6 months = 200905
if today's date = 10-01-2008 then fdate = today's date + 11 months - 6 months = 200904
is it possible to accomplish this in tsql?
March 26, 2008 at 1:47 pm
Isn't today's date + 11 months - 6 months the same as today's date + 5 months?
March 26, 2008 at 1:49 pm
oh, and the T-SQL is really basic:
IF (datepart(month,@MyDate) < 6)
SET @MyDate = ...
ELSE
SET @MyDate = ...
March 26, 2008 at 2:24 pm
If you are wanting the value to be in a query, could you use:
SELECT
CASE
WHEN MONTH(DateColumn) < 6 THEN CAST(YEAR(DateColumn) AS char(4)) + CAST(DATEPART(mm, DATEADD(month, 6, DateColumn)) AS CHAR(2))
ELSE
CAST(YEAR(DateColumn) AS char(4)) + CAST(DATEPART(mm, DATEADD(month, 5, DateColumn)) AS CHAR(2))
END
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply