Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help on an if statement Expand / Collapse
Author
Message
Posted Tuesday, December 11, 2012 3:56 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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+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...
Post #1395348
Posted Tuesday, December 11, 2012 4:13 PM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:54 AM
Points: 708, Visits: 3,290
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
Post #1395350
Posted Tuesday, December 11, 2012 6:30 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1395382
Posted Wednesday, December 12, 2012 9:09 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:54 AM
Points: 708, Visits: 3,290
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 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
Post #1395760
Posted Wednesday, December 12, 2012 1:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:54 AM
Points: 708, Visits: 3,290
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 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
Post #1395871
Posted Wednesday, December 12, 2012 5:15 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 1:27 PM
Points: 1,945, Visits: 3,068
>> 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. <<

SQL is a declarative language, so we avoid using any flow control statements in favor of declarative coding. There is only one format in ANSI/ISO Standard SQL for displaying dates. It is ISO-8601, it is supported by T-SQL now and it is the only one you should be using. Yes, I know that there is still a lot of old 1970's Sybase/UNIX features in T-SQL like CONVERT(); they are now kludges from the days before we understood C/S architectures.

>> 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. <<

NO! NO! SQL has temporal data types and functions. This is not COBOL or BASIC where you have to use strings for dates. The year is 2012, not 1965 :)

Step by step:

We have a function to add years to a date. We have a CASE expression (not statement!). Use them

SET @foobar_date
= CASE WHEN MONTH(@foobar_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 Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1396778
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse