Solved

Datetime

Posted on 2011-03-25
8
581 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
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 40

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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