| 
                                    
                                        |  | SQL Server Error Messages - Msg 298Error MessageServer: Msg 298, Level 16, State 1, Line 1
The conversion from datetime data type to smalldatetime
data type resulted in a smalldatetime overflow error.
 CausesAs the message suggests, you are trying to convert a DATETIMEdata type into aSMALLDATETIMEdata type and the date is either beforeJanuary 1, 1900or afterJune 6, 2079. |  As a reference, a DATETIMEdata type can contain date and time data fromJanuary 1, 1753throughDecember 31, 1999, to an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.003333 seconds).  On the other hand, aSMALLDATETIMEdata type can contain date and time data fromJanuary 1, 1900, throughJune 6, 2079, with accuracy to the minute.SMALLDATETIMEvalues with 29.998 seconds or lower are rounded down to the nearest minute; values with 29.999 seconds or higher are rounded to the nearest minute. Values with the DATETIMEdata type are stored internally by Microsoft SQL Server as two 4-byte integers.  The first 4 bytes store the number of days before or after the base date, January 1, 1900.  The base date is the system reference date.  Values forDATETIMEearlier thanJanuary 1, 1753, are not permitted.  The other 4 bytes store the time of day represented as the number of milliseconds after midnight. On the other hand, the SMALLDATETIMEdata type stores dates and times of day with less precision thanDATETIME.  SQL Server storesSMALLDATETIMEvalues as two 2-byte integers.  The first 2 bytes store the number of days afterJanuary 1, 1900.  The other 2 bytes store the number of minutes since midnight.  Dates range fromJanuary 1, 1900, throughJune 6, 2079, with accuracy to the minute. Here's an example on how the error can be encountered.  Let's assume you have a table that contains the birthdays of the U.S presidents, as follows: CREATE TABLE [dbo].[US President] (
    [Name]        VARCHAR(50),
    [Birthday]    DATETIME
)
Name                 Birthday
-------------------- -----------
George Washington    02/22/1732
John Adams           10/30/1735
Thomas Jefferson     04/13/1743
James Madison        03/16/1751
James Monroe         04/28/1758
Abraham Lincoln      02/12/1809
Ulysses S. Grant     04/27/1822
Grover Cleveland     03/18/1837
Woodrow Wilson       12/28/1856
William Taft         09/15/1857
Theodore Roosevelt   10/27/1858
Franklin Roosevelt   01/30/1882
Harry Truman         05/08/1884
Dwight Eisenhower    10/14/1890
The following SELECT statement will give you the error: SELECT [Name], CAST([Birthday] AS SMALLDATETIME) AS [Birthday]
Server: Msg 298, Level 16, State 1, Line 1
The conversion from datetime data type to smalldatetime data type resulted in a smalldatetime
overflow error.
 Another way of encountering the error is when changing the data type of the column from a DATETIMEintoSMALLDATETIMEusing theALTER TABLEand there are values in the column that is either beforeJanuary 1, 1900or afterJune 6, 2079: ALTER TABLE [dbo].[US President]
ALTER COLUMN [Birthday] SMALLDATETIME
Server: Msg 298, Level 16, State 1, Line 1
The conversion from datetime data type to smalldatetime data type resulted in a smalldatetime
overflow error.
The statement has been terminated.
 Solution / Work AroundIf your DATETIMEdata type column contains values beforeJanuary 1, 1900or afterJune 6, 2079, then you don't have a choice but to leave it asDATETIMEdata type and will not be able to change it to aSMALLDATETIMEdata type.  To identity rows in your table that cannot be converted toSMALLDATETIME, you can easily check the dates and see if it is beyond theSMALLDATETIMEranges: SELECT * FROM [US President]
WHERE [Birthday] < '01/01/1900' OR [Birthday] > '06/06/2079'
 If you still want to convert the column into SMALLDATETIMEwhen returning from aSELECTstatement and just returnNULLif the value cannot be converted toSMALLDATETIME, then you can do the followingSELECTstatement: SELECT [Name], CASE WHEN [Birthday] < '01/01/1900' OR
                         [Birthday] > '06/06/2079' THEN NULL
                    ELSE [Birthday] END AS [Birthday]
FROM [dbo].[US President]
 |