C# Datetime Issue to Sql - c#

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.

Related

How to reformat Datetime when retrieving it from database using linq

I insert Datetime value into a SQL Server database with the following format: dd-MM-yy HH:mm. If I insert 01-01-18 15:30 into database and execute command
select datetimeColumn from mytable
I get back 2018-01-01 15:30:00.000. But if I retrieve all records of the table which contains the column with datetime type like
meetings = meetings.Where(m => m.meeting_name.ToLower().Contains(searchString.ToLower()));
datetime values become like 01-Jan-18 3:30:00 PM.
How can I keep datetime format the same as the format that I've inserted?
SQL Server stores data as two intergers, the first one reserved for the date and the 2nd one for the time.
meetings = meetings.Where(m => m.meeting_name.ToLower().Contains(searchString.ToLower()));
So by querying you just read date-time as is! All you need to do is to format datetime in your desired string format, for example:
var dateTime = DateTime.Now;
var dateTimeFormmated = dateTime.ToString("yyyy-MM-dd hh:mm:ss.ffff");
// 2018-01-26 05:52:54.3250
SQL Server stores the data as one or more integers, depending on the data type. Numbers are what being stored, not formatted strings. The same applies to other database providers for datetime/timestamp database types.
For instance DATETIME data type. According to SQL Server documentation, the database engine stores a DATETIME value as two integers. The first integer represents the day and the second integer represents the time. The days can range from January 1, 1753, through December 31, 9999, and the times can range from 00:00:00.000 through 23:59:59.997, with the default value being 1900-01-01 00:00:00.000.
The formatted string representation you see in say sql management studio or in any application, is done by the application showing the sql query result for you. This means that the burden of supporting different date/time formats lies with the application doing sql commands.

Datetime in C# - why doesn't milliseconds show and get subsequently stored in SQL Server?

Why doesn't milliseconds show in the datetime and get subsequently stored in SQL Server?
It shows as 11/14/2017 3:46:14 PM. The milliseconds are there - 323 but not part of the datetime.
So I want it stored in the SQL Server table as 11/14/2017 3:46:14.323 PM.
However, it stores it as 11/14/2017 3:46:14 PM.
The table column is dateTime.
Here's a debug shot of the date in the C# code.
Here's the table;
It's just a display format problem.You can use this code in SSMS:
SELECT CAST ('11/14/2017 3:46:14.323 PM' AS DATETIME)
You will see millseconds. In fact,the data stored is the same.

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.

Default getdate() always stores AM for time in SQL Server

I have default value getdate() in the blog table in SQL Server database
In fact, I am not even sure if it does store AM or PM in the field
The column date_created is of type datetime and default value is (getdate())
how can I store AM or PM in the field?
The database is not storing AM or PM... it is storing a specific point in time. How you represent that point in time visually (through 24-hour time or 12-hour time) is up to how you do your formatting.
The column date_created is of type datetime
That means it's stored as a binary value that is agnostic about AM/PN and 12/24 hours. All that only happens when you ask for a string representation.
So it could be just the configuration of your App or tools.
Try something like this:
SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
That will yield "Nov 21 2011 1:29PM". See here for more formats.

Categories