如何從字符串日期時間獲得年份(2009)和月份(12),以下給出正確的完整日期但錯誤的年份(1905-07-03 00:00: 00.000)和月份(1900-01-13 00:00:00.000)。我曾嘗試將YYYY更改爲年份和月份。將字符串轉換爲日期時間爲年份和月份SQL
Declare @date dateTime;
Declare @CurrentYear datetime;
Declare @CurrentMonth datetime;
Select @date = CONVERT (datetime , '20091231' ,112);
Select @CurrentYear = DATEPART(YYYY,@date);
--Select @CurrentYear = YEAR(@Date); <---- still wrong year
Select @CurrentMONTH = DATEPART(MM,@date);
--Select @CurrentMonth = MONTH(@date); <---- still wrong year
select @date as fulldate, @CurrentYear as [year], @CurrentMonth as [Month];
到目前爲止,沒有任何SO建議工作。
問候 ķ
謝謝,改變了使用ISO 8601的代碼。 – TheOCD 2010-02-11 18:32:52