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 @ 3:24 PM
Points: 17,812, Visits: 15,737
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: Friday, October 10, 2014 7:53 AM
Points: 220, Visits: 1,163
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 @ 3:24 PM
Points: 17,812, Visits: 15,737
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
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:07 PM
Points: 2,194, Visits: 3,304
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)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
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, October 10, 2014 7:53 AM
Points: 220, Visits: 1,163
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 @ 10:47 PM
Points: 941, Visits: 2,937
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, October 10, 2014 7:53 AM
Points: 220, Visits: 1,163
I'm using:
DECLARE @CutOff DATE
SET @CutOff = '01/01/2016'

Post #1599210
Posted Friday, August 8, 2014 10:17 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 8:11 AM
Points: 52, Visits: 121
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 @ 6:48 PM
Points: 20,734, Visits: 32,499
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, October 10, 2014 7:53 AM
Points: 220, Visits: 1,163
DOB
Post #1601318
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse