OB1Canobie
asked on
Convert varchar data to datetime
I have two tables in MS SQL Server. The source field is a varchar data type and has data that I need to move to another table field of DateTime data type. I need to convert the varchar to a DateTime format. The data in the source file is in the format yyyymmdd. How can I get the data to append to the new table without getting data type errors?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
are you sure that "Stage_Call_History" contains the date only in that format ?
run this to find invalid dates
SELECT
INSTANCE_ID,
CALL_DATE
FROM Stage_Call_History
where ISDATE(CALL_DATE) = 0
run this to find invalid dates
SELECT
INSTANCE_ID,
CALL_DATE
FROM Stage_Call_History
where ISDATE(CALL_DATE) = 0
ASKER
Ok, that returned a row with a non-date format, anyway that we can get rid of that line? I wil need to run this routine every night and will not have time to review the data to ensure data is correct evertime.
INSERT INTO
Call_History(
INSTANCE_ID,
Call_Date
)
SELECT
INSTANCE_ID,
CALL_DATE
FROM Stage_Call_History
where ISDATE(CALL_DATE) = 1
Call_History(
INSTANCE_ID,
Call_Date
)
SELECT
INSTANCE_ID,
CALL_DATE
FROM Stage_Call_History
where ISDATE(CALL_DATE) = 1
ASKER
Syntax error converting datetime from character string." I've attached my sql syntax.
Open in new window