• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
15 views
the conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
by (3.5k points)  
retagged by

3 Answers

0 votes

Issue:

Int => Varchar => Date

• Complete waste of resources

• If you must do this, you must validate int values .

SQL Server isn't going to catch that you're passing in an invalid integer

value because 20150231 is a valid integer => 20,150,231

The most expensive filter is ad-hoc.

Solution:

DECLARE @baddate  INT = 20150631=  @casted VARCHAR(12)

SELECT @casted m CAST@baddate  AS VARCHAR(12 )

SELECT CAST(@casted AS DATE) FinalDate

by (220 points)  
0 votes
The main reasons may be C# DateTime object is "bigger" than SQL's smalldatetime type.

Please try this:

Convert  column type from smalldatetime to datetime

Write own SQL Command rather than using EF
by (1.5k points)  
0 votes

I found this bug while working with the Entity Framework and SQL Server 2012. 

It is a SqlException, generated by the SqlClient provider.The SQL Server datetime datatype is capable for putting away dates in the range 1753-01-01 to 9999-12-31, or Jan first 1753 to more much longer.

The datetime2 information type was presented in SQL Server 2008. 

You can say that .Net datetime is isomorphic with the SQL Server datetime2 type. 

The main cause of this error is the default value for an incentive for a uninitialised datetime variable is DateTime.MinValue, or 0001-01-01. 

Some of the fixes or solutions for this bug may be following:

1. Change the capacity to DateTime2 in the database. 

modelBuilder.Entity<MyContent>()

    .Property(f => f.DateModified)

    .HasColumnType("datetime2");

2.Convert the property to be Nullable

public DateTime? DateModified { get; set; }

3. Put a default value within an acceptable range

public class Test

{

    public Test()

    {

        CreatedDate = DateTime.Now;

    }

    public int EmpID { get; set; }

    public DateTime CreatedDate { get; set; }

}

by (1.4k points)  
...