## Need help on an if statement

 Author Message montse 22199 Grasshopper Group: General Forum Members Points: 19 Visits: 63 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+1440IF(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... Abu Dina SSCertifiable Group: General Forum Members Points: 7279 Visits: 3325 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 datetimeset @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 @somedateset @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 lensSociety 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 dwain.c SSC-Forever Group: General Forum Members Points: 44223 Visits: 6431 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 ENDFROM Dates` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!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?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables montse 22199 Grasshopper Group: General Forum Members Points: 19 Visits: 63 Thanks a lot!! this one is working fine!! Now I have another question 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 Abu Dina SSCertifiable Group: General Forum Members Points: 7279 Visits: 3325 montse 22199 (12/12/2012)Thanks a lot!! this one is working fine!! Now I have another question 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 lensSociety 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 montse 22199 Grasshopper Group: General Forum Members Points: 19 Visits: 63 Thanks a lot!!! it worked fine!!! :-):-D;-) Abu Dina SSCertifiable Group: General Forum Members Points: 7279 Visits: 3325 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 lensSociety 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 dwain.c SSC-Forever Group: General Forum Members Points: 44223 Visits: 6431 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!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?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables