I have this query :
set IDENTITY_INSERT dbo.OtherData1 ON
INSERT INTO OtherData1 (OtherDataID, EmployeeID, OtherDate, OType, OSubject, StatementNo, StatementDate, Info, OtherAs, FolderSerial)
SELECT OtherDataID, EmployeeID,
CONVERT(DATE, OtherDate, 103),
OType, OSubject, StatementNo,
CONVERT(DATE, StatementDate), Info,
CASE OtherAs
WHEN 'f' THEN 1
WHEN 's' THEN 2
WHEN 't' THEN 3
WHEN 'f' THEN 4
WHEN 'p' THEN 5
WHEN 'o' THEN 6
ELSE NULL END
, FolderSerial
FROM OtherData
I when I execute it I get this error :
Msg 241, Level 16, State 1, Line 5
Conversion failed when converting date and/or time from character string.
the values for StatementDate, OtherDate are in this format "31/1/1994" as string before convert
What is the problem?
EDIT:
if I added this line of code :
CASE WHEN ISDATE(OtherDate) = 1 THEN
CONVERT(DATE, OtherDate, 103)
ELSE NULL END
I tried it and it’s not working, I need some thing like it to check if vaild do the convert if not insert NULL value and I get the same error message
ISDATE won’t work since you cannot even specify a date format as you can with CONVERT. The following is a VERY elaborate test to reformat valid dates into YYYYMMDD for which ISDATE works predictably.
Comment out the last line
WHERE ISDate(YYYYMMDDToTest) = 0to see what it does with each date.EDIT
You can turn this into a function that replaces the ISDATE – but for the SPECIFIC format [d]d/[m]m/yyyy.