Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Numeric String to date

Status
Not open for further replies.

IanWaterman

Programmer
Jun 26, 2002
3,511
GB
I have a string date stored as

ddmmyy

When I use cast with the field = 080912

Cast(EffectiveDate as Date) I get 2008-09-12 00:00:00

I have solved this by breaking down

cast(SUBSTRING(EffectiveDate,5,2)+SUBSTRING(EffectiveDate,3,2)+SUBSTRING(EffectiveDate,1,2) as DATE)EffDate,

This gives me the correct result but looks a bit convoluted is there a more efficient way?

Thank you

Ian
 
Code:
DECLARE @Test varchar(20)
SET @Test = '080912'

SELECT CAST(@Test as date)
Of course you ALWAYS should check if the string is correct date, because this:

Code:
DECLARE @Test varchar(20)
SET @Test = '083112'

SELECT CAST(@Test as date)
will raise an error

Borislav Borissov
VFP9 SP2, SQL Server
 


Wow! Why would any thinking person store a date with either MONTH or DAY in the first position. WORTHLESS for sorting, not to mention the inability to do date arithmetic with TEXT!

Also it seems suprising that any company in the twentyfirst century would be using TWO CHARACTER DATES. Shouldn't that have been addressed prior to Y2K?

Are they still using a 360 or have they stepped up to a 370? Hollerith cards or green screens? ;-)

Ian, I feel sorry for you having to deal with this.

Amazing!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
OOOPS!!!
Sorry,
I just read what you want as a result.



Borislav Borissov
VFP9 SP2, SQL Server
 
There's likely to be a dozen ways to do this, but every method will seem convoluted. A string of 6 digits will always be interpreted as ymd. Therefore, complex and convoluted is the only way to go.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top