Why can i insert MaxDate but not MinDate - c#

when inserting into my table with a nullable datetime column, inserting DateTime.MinDate raises the error:
"The conversion of a char data type to
a datetime data type resulted in an
out-of-range datetime value."
Yet when i do MaxDate it works fine?
I actually want to insert this value as null but in PropertyInfo.SetValue() passing null value is just automatically setting as MinDate, any suggestions?

Sql Server DateTime has a range of 1753-01-01 through 9999-12-31, while .NET's DateTime from 0001-01-01 12:00:00 midnight to 9999-12-31 11:59:59 P.M.
So DateTime.MinValue is lower then the Sql Server's minimal value, while DateTime.MaxValue fits into the Sql Servers DateTime.
Use nullables: DateTime? to be able to have a null in memory.

Related

Convert Datetime string to date in SQL Server

I have a datetime data from C# like this
2019-03-20T11:25:32.0342949
I tried to convert it to datetime using cast and it triggers error
select cast('2019-03-20T11:25:32.0342949' as date)
Conversion failed when converting date and/or time from character string.
I guess its because of the T in the string.
I tried format also which of course doesn't work because its not identify it as date.
So how can I convert it properly to date. Without some substring methods to extract the date part.
You have to use DATETIME2 instead of DATETIME:
SELECT CAST('2019-03-20T11:25:32.0342949' AS DATETIME2) -- 2019-03-20 11:25:32.0342949
demo on dbfiddle.uk
The issue is the precision of the milliseconds part of your string value. You are using seven digits on the milliseconds part which is not possible on DATETIME. So you can do two things:
shorten the milliseconds part to three digits and use DATETIME
use DATETIME2 for more precision
Use the time, date, datetime2 and datetimeoffset data types for new work. These types align with the SQL Standard. They are more portable. time, datetime2 and datetimeoffset provide more seconds precision. datetimeoffset provides time zone support for globally deployed applications.
source: https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql
There is also a comparison between DATETIME and DATETIME2 on StackOverflow:
DateTime2 vs DateTime in SQL Server
If you need only date this will work
select cast('2019-03-20T11:25:32.0342949' as date) As DATE
If you need date and time this will work
select cast('2019-03-20T11:25:32.0342949' as datetime2) As DATE
Tried in Sql 15 Its working
You have to take advantage of the CONVERT() method. For example, SELECT CONVERT(date, getdate()), with date being the string you just mentioned. In your case, your datetime string takes up 10 letters of string, so you could also do SELECT CONVERT(VARCHAR(10), GETDATE(), 103). The 3rd parameter is the datetime style you want to convert to.
You should not pass datetime as string from C#, this is the correct way to pass:
string sql = "SELECT * FROM table WHERE datevalue= #date";
SqlParameter dateParam = new SqlParameter("#date", SqlDbType.DateTime);
dateParam .Value = dateValue;
SqlCommand command = new SqlCommand(sql);
command.Parameters.AddWithValue("#date", dateParam );
// then execute the command...

C# Datetime Issue to Sql

I am working on C# SQL.
I've a datetime value "1/10/2016 12:00:00 AM"
for eg
Datetime dt="1/10/2016 12:00:00 AM"
When I passed this value to SQL stored procedure as parameter, it changed to 10 Jan 2016
I need the result as 1 OCT 2016 in SQL. Sql stored procedure parameter data type is datetime.
Many Thanks
Change the datetime value and see what happened. If still coming the 10 Jan 2016, then it may be changes that stored procedure is taking the default value which is store value is 10 Jan 2016.
Use parameters to pass the values to sql query. Or use myDateTime.ToString("s"); this for datetime value
"s" - Standard datetime format
Well, are you sure that the value in c# is in fact 1 OCT 2016 and not 10 Jan 2016?
The row:
Datetime dt = "1/10/2016 12:00:00 AM";
Doesn't even compile.
Best practice for passing DateTime values between c# and Sql Server is using sql parameters of type DateTime or DateTime2 and passing an instance of the DateTime struct.
If you are doing that, then the DateTime values are guaranteed to pass correctly.

Error Converting DateTime to Date

I am calling a stored procedure from a WCF service using Linq-to-sql. The function signature is defined in the designer file as:
public int MS_SetTimeKeeperRecord( ... global::System.Data.Linq.Mapping.ParameterAttribute(Name="ReportDate", DbType="Date")] System.Nullable<System.DateTime> reportDate, ...)
The parameter passed to the reportDate is a C# DateTime type variable, and there is no such thing as a Date type variable. I am getting the following error from the call:
The conversion of a date data type to a datetime data type resulted in an out-of-range value
The input field is not null.
How can I make this work?
C# DateTime MinValue is 00:00:00.0000000, January 1, 0001. but if I'm not mistaken SQL min datetime value is 1753-01-01 00:00:00.000. You could get this error if you're trying to pass a value less than SQL min datetime. Also SQL max datetime is 9999-12-31 23:59:59.997 but C# max DateTime is 23:59:59.9999999, December 31, 9999.
Update:
The error is for converting from date to datetime. Date in SQL has different limits to datetime, its range is 0001-01-01 through 9999-12-31 but datetime's range is as above. So whatever the value is, it's out of range for the datetime type, although as it's mentioned in the comments you're using date.
http://msdn.microsoft.com/en-us/library/bb630352.aspx
http://msdn.microsoft.com/en-us/library/ms187819.aspx

Convert C# Datetime to 0000-00-00 00:00:00 to MySQL

DateTime field is 'not null' in Table Structure but I want to store data (MVC C# Datetime) 0000-00-00 00:00:00 to MySQL Table without changed table structure.
I Try to do this but its but ERROR!!
Convert.ToDateTime("0000/00/00");
Please HELP , Thanks
The minimum value for .NET DateTime is January 1, 0001; the minimum for MySQL DATETIME is '1000-01-01', but in case of SQL MODE it is possible to insert '0000-00-00' as DATE, see NO_ZERO_DATE in documentation.
If you want to store '0000-00-00' in .NET DateTime structure, then use '0001-01-01', then if it is possible change this value in representation layer.
If you want to store '0000-00-00' in MySQL, then you should check SQL MODE in MySQL server -
SELECT * FROM information_schema.GLOBAL_VARIABLES
WHERE VARIABLE_NAME = 'sql_mode';
From documentation: NO_ZERO_DATE - In strict mode, do not permit '0000-00-00' as a valid date. You can still insert zero dates with the IGNORE option. When not in strict mode, the date is accepted but a warning is generated.
Server SQL Modes
You need to insert those values as string
Like this
Create table tbl(dt datetime);
Insert into tbl values('0000-00-00 00:00:00');
SELECT CAST(dt as char) FROM tbl
Fiddle
If you're asking how to create a C# DateTime object with 0000-00-00 00:00:00 - you can't, it's an invalid date. You could use DateTime.MinValue though. From memory that's 0000-01-01 00:00:00
Why not use NULL? Using magic values instead of NULL is not always a good idea.

C#+linq - Inserting DateTime value to the Db in the right format

I have a Db server with DateTime fields in the format of "yyyy-MM-dd mm:hh:ss"
I'm trying to use linq2sql to insert a DateTime member to a DateTime field in one of my tables.
When I do it in SQL I convert the DateTime as following:
"Insert into .... Convert(datetime, getdate(), 120) ..."
But when I try to submit the object from Linq the date time inserts in the wrong format.
Is there a way to define the format that will be inserted to the Db in Linq?
Or is it a DateTime object Issue?
You shouldn't be dealing with a string format when you pass dates and times to the database, any more than you would if you were passing a number. The database should be handling all this for you without any conversions. This is true whether you're using LINQ or within the SQL - almost any time you have to manually do string conversions between types at the database level, you should look for a better solution.
If you read the value back out of the database (as a DateTime again) does it have the right value? If not, in what way is it wrong?

Categories