?
Solved

convert varchar to datetime

Posted on 2009-05-18
3
Medium Priority
?
582 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

777 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