SQL DateTime conversion

I am trying to copy a varchar field in a source table to a new table field that is DateTime.  I cannot complete the process because I get an error "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value." How can I find the data causing me problems?
OB1CanobieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
start posting your code here ;)
0
Aneesh RetnakaranDatabase AdministratorCommented:
find those incvalid dates with this query

select datecolumn
from yourTable
where isdate(datecolumn) = 0
0
HainKurtSr. System AnalystCommented:
for example convert a string to datetime, here are some samples

SELECT convert(datetime, '10/23/2016',          101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23',          102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016',          103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016',          104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016',          105) -- dd-mm-yyyy

check here : http://www.sqlusa.com/bestpractices/datetimeconversion/

for more options...
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

OB1CanobieAuthor Commented:
When I ran the following query, it returned times that are marked as invalid.  Why would these be invalid?  I have attached some results.  These are not dates, but time stamps.

215235
215235
215235
215239
215250
215250
215258
215258
215303
215325
215330
215333
215404
215406
215408
USE
Dialer

SELECT
CALL_TIME

FROM
Stage_Dialer_History

WHERE 
isdate(CALL_TIME) = 0

ORDER BY
CALL_TIME ASC

Open in new window

0
OB1CanobieAuthor Commented:
Is the error happening because there is no date and just time information?
0
Aneesh RetnakaranDatabase AdministratorCommented:
>These are not dates, but time stamps

So how will you interpret this
215235

is that in hhmmss format
0
OB1CanobieAuthor Commented:
Yes, the data is in hhmmss.  How do I insert this information without a date, or do I need to insert a date with the time?
0
Aneesh RetnakaranDatabase AdministratorCommented:
you cannot insert only time part alone into the datatime column, all you can do is insert it as a datetime (so it will be 1900-01-01 21:52:35 )
0
OB1CanobieAuthor Commented:
So, I should combine a date and time field into one?  I'm guessing I will need to add the hyphens, spaces, and colons to make the insert work correctly?
0
Aneesh RetnakaranDatabase AdministratorCommented:


SELECT CAST ('1900-01-01 '+ STUFF(STUFF( CALL_TIME, 5,0,':' ), 3,0, ':' ) as datetime )
FROM Stage_Dialer_History
WHERE  isdate(CALL_TIME) = 0
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
As Aneesh said.

You could do this :

              USE Dialer;

SELECT DATEADD(second, CALL_TIME * 36/100 + (CALL_TIME % 10000) * 6/10 + ((CALL_TIME % 10000) % 100), 0)
FROM Stage_Dialer_History
WHERE isdate(CALL_TIME) = 0
ORDER BY CALL_TIME ASC            
;

Hope that helps.
0
Kevin CrossChief Technology OfficerCommented:
Good job, Aneesh.
OB1Canobie: (firstly, I love that name!) You may also want to double check that you don't have a CALL_DATE field in the same table.  Might be helpful if you have lots of data for long periods of time, putting all under one date may be an issue later. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.