Issue: COMPLETE_DATE
column is of type varchar
(it is loaded like this, I cannot change this), I need to convert it to a Numeric
datatype for manipulation. Format of date is yyyy-mm-dd
.
When I run the convert function, I get this error:
Error converting data type varchar to numeric
Solution attempt 1: I ran the LEN
function and noticed I was getting 11 returned instead of the expected 10. Initially I thought it was perhaps extra spaces so I
CONVERT(NUMERIC, RTRIM(LTRIM(COMPLETE_DATE)))
but it still errors out.
Solution attempt 2:
CONVERT(numeric, REPLACE(LTRIM(RTRIM([COMPLETE_DATE])), ' ', ''))
I am running a script from my predecessor where converts this column using the convert function but I am not sure at this point it is any extra space, or maybe special characters hiding, what else can I do to cleanse the column to just the string yyyy-mm-dd
?
UPDATE tbl
SET ACADEMIC_YEAR = CASE WHEN
CONVERT(NUMERIC,SUBSTRING(COMPLETE_DATE,1,4)+SUBSTRING(COMPLETE_DATE,6,2)+SUBSTRING(COMPLETE_DATE,9,2)) >= CONVERT(NUMERIC,SUBSTRING(COMPLETE_DATE,1,4)+'0701')
THEN CONVERT(NUMERIC,SUBSTRING(COMPLETE_DATE,1,4))
ELSE CONVERT(NUMERIC,SUBSTRING(COMPLETE_DATE,1,4))-1 END;