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: Today @ 4:52 PM
Points: 17,654, Visits: 15,507
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
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: Friday, September 19, 2014 10:38 AM
Points: 219, Visits: 1,157
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: Today @ 4:52 PM
Points: 17,654, Visits: 15,507
Nice answer Eirikur



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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1598489
Posted Thursday, July 31, 2014 5:38 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:56 PM
Points: 2,110, Visits: 3,172
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)

"We came in spastic, Like tameless horses /
We left in plastic, As numbered corpses / ...
Remember Charlie, Remember Baker /
They left their childhood On every acre /
And who was wrong? And who was right? /
It didn't matter in the thick of the fight." : the inimitable Mr. Billy Joel, about the Vietnam War
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: Friday, September 19, 2014 10:38 AM
Points: 219, Visits: 1,157
Eirikur worked perfect. It did exactly what I needed it to do. Thanks again!
Post #1598545
Posted Monday, August 4, 2014 4:03 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 5:49 AM
Points: 907, Visits: 2,870
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: Friday, September 19, 2014 10:38 AM
Points: 219, Visits: 1,157
I'm using:
DECLARE @CutOff DATE
SET @CutOff = '01/01/2016'

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

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:29 AM
Points: 40, Visits: 104
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: Today @ 4:30 PM
Points: 20,685, Visits: 32,291
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: Friday, September 19, 2014 10:38 AM
Points: 219, Visits: 1,157
DOB
Post #1601318
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse