SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Missing Century in Date


Missing Century in Date

Author
Message
SQLSeTTeR
SQLSeTTeR
SSC Eights!
SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)

Group: General Forum Members
Points: 955 Visits: 1403
Does anyone have a script handy to fill in a century into a date string. Right now, I'm getting dates in the following format:

7/26/29 = converts to 2029.

I'm looking for a SQL statement that will now to put a 19 or 20 in the century.
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13641 Visits: 11848


select [Date] = convert(date,'7/26/29')




Results:
Date
----------
2029-07-26

SQLSeTTeR
SQLSeTTeR
SSC Eights!
SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)

Group: General Forum Members
Points: 955 Visits: 1403
Yes but the birthday is 1929. That is the problem.
Keith Tate
Keith Tate
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2196 Visits: 979
You can configure your two digit year cutoff so that it displays the correct century. http://msdn.microsoft.com/en-us/library/ms191004.aspx



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38285 Visits: 19417
Further on Michael's excellent advice, the CONVERT function takes the third optional parameter [style], controls the style of the input/output.
Cool
select [Date] = CONVERT(VARCHAR(10),convert(date,'7/26/29',1),101)



Results
07/26/2029


European
select [Date] = CONVERT(VARCHAR(10),convert(date,'7/26/29',1),103)



Result
26/07/2029

SQLSeTTeR
SQLSeTTeR
SSC Eights!
SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)

Group: General Forum Members
Points: 955 Visits: 1403
Gents - the birthday is 1929 not 2029. I would prefer not to update the server because I don't now the impact.
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38285 Visits: 19417
SQLSeTTeR (7/31/2014)
Gents - the birthday is 1929 not 2029. I would prefer not to update the server because I don't now the impact.


Sorry, didn't notice the century before, looks like something's wrong there, my settings where a ran the code is
configuration_id   name   value   minimum   maximum   value_in_use   description   is_dynamic   is_advanced
1127 two digit year cutoff 2049 1753 9999 2049 two digit year cutoff 1 1

, still 7/26/29 results in 7/26/2029.
Cool
Run this code to check your settings
exec sp_configure 'show advanced options',1;RECONFIGURE;
select * from sys.configurations where name = 'two digit year cutoff';
exec sp_configure 'show advanced options',0;RECONFIGURE;



Edit: Ouch...Blush
I'm being silly here, anything before the setting is interpreted as this century, anything after is last century.

You will have to assess the impact carefully before changing the settings.
Cool
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89622 Visits: 38932
SQLSeTTeR (7/31/2014)
Does anyone have a script handy to fill in a century into a date string. Right now, I'm getting dates in the following format:

7/26/29 = converts to 2029.

I'm looking for a SQL statement that will now to put a 19 or 20 in the century.


First, SQL Servers default configuration is that when given a 2 digit century anything less than 50 is considered 20+ and anything greater than or equal to 50 is 19+.

Showing us just the date 7/26/29 really tells us nothing. You later posts tell us this is a birthday, 7/26/1929. How were we to know?

Still, you have given use little information to go on to really help. There is no magical function to add 20 or 19 to a date. Without a consistent rule to write such a function again, not much we can do to help.

So what is the criteria for determining if the year belongs in the 1900's or 2000's?

Cool
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)
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38285 Visits: 19417
Quick fix for your update
Cool
declare @mycutoff date = '01/01/2020';

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



Result
26/07/1929

SQLSeTTeR
SQLSeTTeR
SSC Eights!
SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)

Group: General Forum Members
Points: 955 Visits: 1403
This is exactly what I needed. Thank you!

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

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