Solved

Datetime

Posted on 2011-03-25
8
582 Views
Last Modified: 2012-05-11
When trying to cast these as datetime the conversion fails.  How can I convert them to datetime format?

013111
033111
022811
0
Comment
Question by:N R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 35216535
add slashes:


01/31/11
03/31/11
02/28/11

you can use substring to parse it out.
0
 
LVL 11

Author Comment

by:N R
ID: 35216556
How?  I'm trying the below right now and failing
cast(datecolumn as datetime)

Open in new window

0
 
LVL 4

Expert Comment

by:RGBDart
ID: 35216585
First, you must convert this string to one of the supported formats.
For example to  mm-dd-yy  

select convert (datetime, substring('013111', 1,2) + '-' + substring('013111', 3,2) + '-' + substring('013111', 5,2), 10)

Open in new window


will return a valid datetime.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 11

Author Comment

by:N R
ID: 35216596
@ RGBDart:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 35216619
select  cast (substring('013111', 1,2) + '-' + substring('013111', 3,2) + '-' + substring('013111', 5,2) as datetime)
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 35216627
you can try like this.
select CONVERT(datetime,SUBSTRING(str_field,5,2)+LEFT(@date,4))

Open in new window

declare @date varchar(10) 
set @date = '013111'
select CONVERT(datetime,SUBSTRING(@date,5,2)+LEFT(@date,4)) -- 2011-01-31 00:00:00.000
set @date = '033111'
select CONVERT(datetime,SUBSTRING(@date,5,2)+LEFT(@date,4)) -- 2011-03-31 00:00:00.000
set @date = '022811'
select CONVERT(datetime,SUBSTRING(@date,5,2)+LEFT(@date,4)) -- 2011-02-28 00:00:00.000

Open in new window

0
 
LVL 4

Expert Comment

by:RGBDart
ID: 35216647
Gallitin,

try this one

select convert (datetime, substring('013111', 1,2) + '-' + substring('013111', 3,2) + '-20' + substring('013111', 5,2), 110)

The goal is to correctly format your string.
Does this query works on your server
 
select convert (datetime, '01-31-2011', 110)

Open in new window

?
0
 
LVL 11

Author Closing Comment

by:N R
ID: 35216649
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question