Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

convert varchar to datetime

Posted on 2009-05-18
3
Medium Priority
?
583 Views
Last Modified: 2012-05-07
I have the following table
Create table zzNUoSNMIDateRange
(
NMI varchar(10)
, FromDate date
, ToDate date
, Quantity Float
, FRMP varchar(10)
)

and I need to transfer data from another table to this one. Now all the data in the second table is is of the data type VARCHAR(50)

I need to copy this data :
INSERT INTO zzNUoSNMIDateRange
(NMI, FromDate, ToDate)
Select distinct [Column 7],
CAST ( [Column 11] AS datetime),
CAST ( [Column 12] AS datetime)
 from zzExtractNUOS


also data in  [Column 11]  and  [Column 12] is in this format ' 20080826'

 i get this error when i try the conversion,
"Conversion failed when converting date and/or time from character string."
0
Comment
Question by:manivineet
[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 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 600 total points
ID: 24419195
Hi,

There could be numerous reasons to why, so start by showing the offending dates.

SELECT [Column 11] FROM zzExtractNUOS WHERE ISDATE([Column 11])=0
SELECT [Column 12] FROM zzExtractNUOS WHERE ISDATE([Column 12])=0

/peter
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 450 total points
ID: 24419257
please try this code. it that does also fail, you have indeed bad data.
INSERT INTO zzNUoSNMIDateRange
(NMI, FromDate, ToDate)
Select distinct [Column 7], 
CONVERT(datetime, [Column 11] , 112),
CONVERT(datetime, [Column 12] , 112)
 from zzExtractNUOS

Open in new window

0
 
LVL 11

Assisted Solution

by:Muhammad Ousama Ghazali
Muhammad Ousama Ghazali earned 450 total points
ID: 24419271
Try using the following:
INSERT INTO zzNUoSNMIDateRange
(NMI, FromDate, ToDate)
Select DISTINCT [Column 7], 
CONVERT(DATETIME, SUBSTRING(Column 11, 1, 4) + '-' + SUBSTRING(Column 11, 5, 2) + '-' + SUBSTRING(Column 11, 7, 2)),
CONVERT(DATETIME, SUBSTRING(Column 12, 1, 4) + '-' + SUBSTRING(Column 12, 5, 2) + '-' + SUBSTRING(Column 12, 7, 2))
FROM zzExtractNUOS

Open in new window

0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

647 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