I want to convert a string “PERIOD” in this form yyyymmdd (eg: 20120701) into date in SQLServer.
So I used the convert function like this:
CONVERT(DATETIME,PERIOD,102);
and
CONVERT(DATETIME,PERIOD,120);
but I’m getting an
SqlException: “Arithmetic overflow error converting expression to
data type datetime.”This is the whole query, is there any other mistake in this?
"SELECT ACTUAL_N_WELLS, TARGET_N_WELLS, [YTD ACTUAL] AS YTD_ACTUAL, [YTD BUDGET] AS YTD_BUDGET FROM DASH_GL_DRILLING_PERF WHERE ASSET_NAME='Bu Hasa Asset' AND ( CONVERT(DATETIME,PERIOD,112) > (SELECT DATEADD(MONTH,-3,MAX(CONVERT(DATETIME,PERIOD,112))) from DASH_GL_DRILLING_PERF))");
What is the correct way to do this?
If it were a date format issue you would get an out-of-range error, as your getting an overflow error check your not using an integer