date calculation

  • 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?

  • Isn't today's date + 11 months - 6 months the same as today's date + 5 months?

  • oh, and the T-SQL is really basic:

    IF (datepart(month,@MyDate) < 6)

    SET @MyDate = ...

    ELSE

    SET @MyDate = ...

  • 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