Need help on an if statement

  • Hello all.

    Im still learning in t-sql trying to get my certification thats why I am not to familiar wich functions should I use for calculate dates including an if statement.

    The requirement is I have a date which I need to calculate adding 4 years. Then if the month of the date is >7 then I need to standarize the following value "12/31" and concatenate the year of the date.

    I already did the formula in Excel as follows:

    Date1=Date1+1440

    IF(MONTH(Date1)>7,"12/31","7/30")

    CONCATENATE(F4,"/",G4)

    DATEVALUE(H4)

    Example: if the date is "11/1/2012" the results should be "12/31/2016"

    I will appreciate all your help doing the same formula in t-sql.

    Thanks...

  • Possibly a bad attempt at solving this on my tablet. There has to be a cleverer way to do it lol give it a go anyway

    If datepart(mm, your-date) >7

    then cast('31/12/' + cast(datepart(yy, dateadd(yy, 4, your-date)) as char(4)) as datetime)

    tested it this morning - original solution doesn't work lol but here is one that does:

    declare @somedate datetime

    set @somedate = '2012-08-01'

    If datepart(mm, @somedate) >7 select cast('12/31/' + cast(datepart(yy, dateadd(yy, 4, @somedate)) as char(4)) as datetime) else select @somedate

    set @somedate = '2013-01-01'

    If datepart(mm, @somedate) >7 select cast('12/31/' + cast(datepart(yy, dateadd(yy, 4, @somedate)) as char(4)) as datetime) else select @somedate

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • How about something like this?

    ;WITH Dates AS (

    SELECT MyDate='2012-11-01'

    UNION ALL SELECT '2012-04-04')

    SELECT MyDate

    ,CASE WHEN DATEPART(month, MyDate) <= 7 THEN DATEADD(year, 4, MyDate)

    ELSE DATEADD(year, 5+DATEDIFF(year, 0, MyDate), 0)-1 END

    FROM Dates


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thanks a lot!! this one is working fine!! Now I have another question :w00t:

    I already have a view (ED) with the following syntax:

    SELECT ContactId,Effective_Date,Semester,'EffectiveGraduationDate'= DATEADD(year, 4, EGD) from ED

    Instead of 'EffectiveGraduationDate'= DATEADD(year, 4, EGD)

    I need to place the if syntax you gave me...

    How it would be?

    Sorry I am trying hard here 🙁

  • montse 22199 (12/12/2012)


    Thanks a lot!! this one is working fine!! Now I have another question :w00t:

    I already have a view (ED) with the following syntax:

    SELECT ContactId,Effective_Date,Semester,'EffectiveGraduationDate'= DATEADD(year, 4, EGD) from ED

    Instead of 'EffectiveGraduationDate'= DATEADD(year, 4, EGD)

    I need to place the if syntax you gave me...

    How it would be?

    Sorry I am trying hard here 🙁

    Sure, try this:

    SELECT ContactId,Effective_Date,Semester,

    EffectiveGraduationDate= CASE WHEN DATEPART(month, EGD) <= 7 THEN DATEADD(year, 4, EGD)

    ELSE DATEADD(year, 5+DATEDIFF(year, 0, EGD), 0)-1 END

    from ED

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • Thanks a lot!!! it worked fine!!! :-):-D;-)

  • montse 22199 (12/12/2012)


    Thanks a lot!!! it worked fine!!! :-):-D;-)

    Credits go to dwain to be fair lol I used his method it's better than my original solution.

    Good luck!

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • Abu Dina (12/12/2012)


    montse 22199 (12/12/2012)


    Thanks a lot!!! it worked fine!!! :-):-D;-)

    Credits go to dwain to be fair lol I used his method it's better than my original solution.

    Good luck!

    Thanks for the credit Abu!

    Quoting my wife's favorite movie (Black Hawk Down): T'weren't nuthin'!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply