How to reformat Datetime when retrieving it from database using linq - c#

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.

Related

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.

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.

Oracle date formatting

in my c# programme i am requesting data from an oracle database and one field is the date abd time in this format - 12/09/2008 15:11:17 , is there anyway i can just return the date?
Is there also a way of ensuring its in british format, by modifying the sql to be dd/mm/yyyy
thanks
You could get the date part of the DateTime using C#, You could do
string date = MyDateTime.ToString("dd/MM/yyyy");///let MyDateTime be your DateTime variable
If you want to do in Oracle, you can use to_char for example,
select to_char(sysdate, 'dd/MM/yyyy') From dual;
The Oracle trunc() function removes the time part:
select trunc(datecol) from mytable;
In your sql query to oracle you can
to_date('12/09/2008 15:11:17', 'dd/MM/yyyy')
where you replace the date with your field in the oracle db.
Alternatively, you can handle it on the C# side with formatting
CultureInfo ukCulture = new CultureInfo("en-GB");
//this assuming you do not have a datetime type
DateTime myDateTime = DateTime.Parse("12/09/2008 15:11:17", ukCulture.DateTimeFormat);
string result = myDateTime.ToString(ukCulture.DateTimeFormat.ShortDatePattern));

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