Solved

convert varchar to datetime

Posted on 2009-05-18
3
581 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 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 143

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

696 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