Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Need help on an if statement Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, December 11, 2012 3:56 PM
 Grasshopper Group: General Forum Members Last Login: Monday, April 22, 2013 2:08 PM 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...
Post #1395348
 Posted Tuesday, December 11, 2012 4:13 PM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, April 09, 2014 2:57 AM Points: 704, Visits: 3,270
 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
Post #1395350
 Posted Tuesday, December 11, 2012 6:30 PM
 Hall of Fame Group: General Forum Members Last Login: Today @ 2:24 AM Points: 3,591, Visits: 5,100
 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!
Post #1395382
 Posted Wednesday, December 12, 2012 9:09 AM
 Grasshopper Group: General Forum Members Last Login: Monday, April 22, 2013 2:08 PM 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
Post #1395742
 Posted Wednesday, December 12, 2012 9:19 AM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, April 09, 2014 2:57 AM Points: 704, Visits: 3,270
 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
Post #1395760
 Posted Wednesday, December 12, 2012 1:03 PM
 Grasshopper Group: General Forum Members Last Login: Monday, April 22, 2013 2:08 PM Points: 19, Visits: 63
 Thanks a lot!!! it worked fine!!!
Post #1395861
 Posted Wednesday, December 12, 2012 1:25 PM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, April 09, 2014 2:57 AM Points: 704, Visits: 3,270
 montse 22199 (12/12/2012)Thanks a lot!!! it worked fine!!! 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
Post #1395871
 Posted Wednesday, December 12, 2012 5:15 PM
 Hall of Fame Group: General Forum Members Last Login: Today @ 2:24 AM Points: 3,591, Visits: 5,100
 Abu Dina (12/12/2012)montse 22199 (12/12/2012)Thanks a lot!!! it worked fine!!! 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!
Post #1395934
 Posted Friday, December 14, 2012 12:07 PM
 SSCommitted Group: General Forum Members Last Login: Tuesday, January 15, 2013 11:11 AM Points: 1,945, Visits: 2,782
 >> I am still learning T-SQL trying to get my certification that is why I am not to familiar which 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 standardize the following value "12/31" and concatenate the year of the date. < 7 THEN DATEFROMPARTS (YEAR(@foobar_date)+4, 12, 31) ELSE DATEADD(YEAR, 4, @foobar_date) END;The DATEFROMPARTS() is new to SQL Server 2012. You might have to do it with strings and CAST() functions. Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL
Post #1396778

 Permissions