Solved

convert varchar to datetime

Posted on 2009-05-18
3
575 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
3 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 200 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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 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 150 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now