Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help on an if statement


Need help on an if statement

Author
Message
montse 22199
montse 22199
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

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+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...
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)

Group: General Forum Members
Points: 725 Visits: 3323
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

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
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4275 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 END
FROM 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
montse 22199
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 63
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 Sad
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)

Group: General Forum Members
Points: 725 Visits: 3323
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 Sad


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

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
montse 22199
montse 22199
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 63
Thanks a lot!!! it worked fine!!! :-):-D;-)
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)Right there with Babe (725 reputation)

Group: General Forum Members
Points: 725 Visits: 3323
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

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
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4275 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search