SELECT CONVERT(datetime, '20070701', 112) You can get this error Arithmetic overflow error converting expression to data type datetime. that error came because we were comparing a datetime with int without converting later to datetime. like X.PriceDt < A.PrimaryDate this will fix this X.Close...