什麼是日期時間2? (What is datetime2?)


問題描述

什麼是日期時間2? (What is datetime2?)

I´ve got this in a INSERT statment to MSSQL 2008

  

System.Data.SqlClient.SqlException:   The conversion of a datetime2 data   type to a datetime data type resulted   in an out-of-range value.


參考解法

方法 1:

SQLServer's datetime datatype is a much smaller range of allowed values than .net datetime datatype. SQLServer's datetime type basically supports the gregorian calendar, so the smallest value you can have is 1/1/1753. In 2008 SQLServer added a datetime2 datatype that supports back to year 1 (there was no year 0). Sounds like you're trying to insert a datetime value that's before 1/1/1753 into a datetime (not datetime2) SQLServer column

方法 2:

  

Defines a date that is combined with a time of day that is based on 24-hour clock. datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision.

http://technet.microsoft.com/en-us/library/bb677335.aspx

方法 3:

From technet:

  

Defines a date that is combined with a time of day that is based on 24-hour clock. datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision.

I had to check because I thought datetime2 had some relation with varchar2. Apparently, no relation at all.

Put your code so we can guess what caused the problem.

方法 4:

Could it be that your database table has a "DATETIME" or "SMALLDATETIME" column and you're trying to insert an out-of-range date?? DATETIME covers 1753-1-1 through 9999-12-31, while SMALLDATETIME covers 1900-1-1 through 2079-6-6 only.

The new SQL Server 2008 DATETIME2 data type will cover 0001-1-1 through 9999-12-31.

Marc

方法 5:

I got this error when my database column was created as NOT NULL and I specifically specified a Nullable = true on my DateTime Property in my ADO.Entity framework EntityType.

To fix it I make the Nullable property = (None) 

(by GlennWalden LeverichCantillonPaulo Guedesmarc_sAnton Swanevelder)

參考文件

  1. What is datetime2? (CC BY-SA 3.0/4.0)

#sql-server-2008 #SQL #entity-framework #.net #c#-3.0






相關問題

基於集合的插入到具有 1 到 0-1 關係的兩個表中 (Set based insert into two tables with 1 to 0-1 relation)

如何使用 CTE 從分層視圖中獲取元素 (How to get elements from a hierarchical view with CTE)

where 子句中使用等於和 IN 的 CASE 語句 (CASE Statement in where clause using equal to and IN)

與 SQL 服務器匯總匯總 - 但只有最後一個摘要? (Sum with SQL server RollUP - but only last summary?)

將範圍分組到範圍 (Group a range towards a range)

在 SQL Server 2008 中運行 WHILE 或 CURSOR 或兩者 (Running WHILE or CURSOR or both in SQL Server 2008)

按組中的最大值排序 (Order by the max value in the group)

使用存儲過程創建搜索函數 (Creating a Search Function, using stored procedure)

在 sql server 2008 中查詢最後費用日期 (Query for Last Fees Date in sql server 2008)

除了 SQL Server Profiler,還有什麼 SQL Server Profile? (What is SQL Server Profile aside from SQL Server Profiler?)

什麼是日期時間2? (What is datetime2?)

可以在這裡使用 Common Table 表達式來提高性能嗎? (Can Common Table expressions be used here for performance?)







留言討論