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 ««123»»

Missing Century in Date Expand / Collapse
Author
Message
Posted Thursday, July 31, 2014 2:04 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:45 PM
Points: 19,199, Visits: 17,513
I think the best solution is to enforce 4 digit year on the input and in dates coming into the system.

Sure somebody could not have been born yet in 2029. And if you know the column name is known to be of a specific date type (e.g. birthdate, employmentdate, deathdate, order date) then you can throw logic at it. But then you have further complications with maintaining that code and ensuring somebody doesn't change something on you.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #1598468
Posted Thursday, July 31, 2014 2:32 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 20, 2015 1:58 PM
Points: 227, Visits: 1,260
I wish that was the case. We do our best to get a 4 digit (CCYY). It doesn't always work out like that.
Post #1598486
Posted Thursday, July 31, 2014 2:35 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 3:45 PM
Points: 19,199, Visits: 17,513
Nice answer Eirikur



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #1598489
Posted Thursday, July 31, 2014 5:38 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: Yesterday @ 4:22 PM
Points: 3,062, Visits: 4,694
Agreed, nice code.

But if it's a birthday, wouldn't it be reasonable to use the current date + 1 as the cutoff, since there won't be future birthdates but someone could conceivably be 90+ years old?

declare @mycutoff date
set @mycutoff = dateadd(day, 1, getdate())
--yes, I know you can just use getdate()+1, but I prefer to explicitly use DATEADD
...




SQL DBA,SQL Server MVP('07, '08, '09)

"If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them." James Blackburn, in closing argument in the "Fatal Vision" murders trial
Post #1598540
Posted Thursday, July 31, 2014 7:39 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 20, 2015 1:58 PM
Points: 227, Visits: 1,260
Eirikur worked perfect. It did exactly what I needed it to do. Thanks again!
Post #1598545
Posted Monday, August 4, 2014 4:03 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, August 31, 2015 5:19 AM
Points: 1,067, Visits: 3,259
SQLSeTTeR (7/31/2014)
Eirikur worked perfect. It did exactly what I needed it to do. Thanks again!


What about 7/26/19?

declare @mycutoff date = '01/01/2020';

select [Date] = CONVERT(VARCHAR(10),CASE WHEN convert(datetime,'7/26/19',1) >= @mycutoff THEN DATEADD(YEAR,-100,convert(datetime,'7/26/19',1)) ELSE convert(datetime,'7/26/19',1) END,103)





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1599177
Posted Monday, August 4, 2014 5:49 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 20, 2015 1:58 PM
Points: 227, Visits: 1,260
I'm using:
DECLARE @CutOff DATE
SET @CutOff = '01/01/2016'

Post #1599210
Posted Friday, August 8, 2014 10:17 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:37 AM
Points: 220, Visits: 287
Ok, I just have to toss a wrench into the logic here.

Since we are talking about birthdays, what if the person is over 100 years old? This logic will fail.
Post #1601259
Posted Friday, August 8, 2014 10:46 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:04 PM
Points: 22,099, Visits: 34,993
What type of data is being stored in the database? What is the application it is supporting?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1601270
Posted Friday, August 8, 2014 1:20 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 20, 2015 1:58 PM
Points: 227, Visits: 1,260
DOB
Post #1601318
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse