工作中遇到一個問題,A表中字段(DateTime1)的數據類型為DateTime,新建了一張表B的SMALLDATETIME1字段的數據來自A表的DateTime1

通過以下兩篇文章知道DateTime與smalldatetime的差別(smalldatetime僅Sqlserver2005以上版本支持,2005不支持)
DateTime時間范圍"1753-01-01 00:00:00.000"到"9999-12-31 23:59:59.997"
smalldatetime時間范圍"1900-01-01 00:00:00"到"2079-06-06 23:59:00"
datetime
Date and time data from January 1, 1753 through December 31, 9999, to an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007 seconds, as shown in the table.
smalldatetime
Date and time data from January 1, 1900, through June 6, 2079, with accuracy to the minute. smalldatetime values with 29.998 seconds or lower are rounded down to the nearest minute; values with 29.999 seconds or higher are rounded up to the nearest minute.
Date and time types in SQL Server


--如果存儲過程存在,則刪除重建 IF EXISTS(select1from sys.objects where type='p' AND name='HTL_Convent_DateTime') DROP PROCEDURE HTL_Convent_DateTime; --必須加上Go,否則下面創建存儲過程時會出現錯誤"MSSQL 'CREATE/ALTER PROCEDURE' 必須是查詢批次中的第一個語句。" GO --對輸入的日期進行各種日期格式轉換 --HLT --'2014-07-30 15:12:17' CREATE PROCEDURE HTL_Convent_DateTime @date_time DATETIME AS BEGIN SELECT @date_time AS 'DateTime', CAST (@date_time AS DATETIME2) AS 'DateTime2' , CAST (@date_time AS DATE) AS 'DATE' , CAST (@date_time AS TIME) AS 'TIME' , CAST (@date_time AS datetimeoffset) AS 'datetimeoffset' SELECT CAST (@date_time AS SMALLDATETIME)AS 'SMALLDATETIME'; END GO
1900-01-01之前的日期無法從DateTime轉換成smalldatetime, smalldatetime時間范圍"1900-01-01 00:00:00"到"2079-06-06 23:59:00"



DECLARE @date DATETIME SET @date='1753-01-01 00:00:00.000' SELECT CAST (@date AS SMALLDATETIME);
