You do not have to change the data in the database.
If the database column data type is
DateTime
, then the dates are stored in the
DateTime
format not in a string format. It is the proper thing to do to store dates in a
DateTime
format. Month-first or day-first is a display choice not a choice you have on how the date is stored. The DateTime data type stores dates in an internal format that allows the value to be formatted in many different ways after it is retrieved from the database.
When the dates are retrieved into your C# program as a DateTime variable, they can be formatted in many different ways by your C# application. Here are some examples:
DateTime dtVar;
Console.WriteLine(dtVar.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine(dtVar.ToString("MM-dd-yyyy"));
Console.WriteLine(dtVar.ToString("MM/dd/yyyy"));
Console.WriteLine(dtVar.ToString("dd-MM-yyyy"));
Console.WriteLine(dtVar.ToString("dd/MM/yyyy"));
If you want to return the date from SQL as a string (which I do not recommend), then you can use the
Convert
function. Here is an example of converting the same date into four different strings:
select convert(varchar(10),reported_date,103), convert(varchar(10),reported_date,101),convert(varchar(10),reported_date,105),convert(varchar(10),reported_date,110) from inventory_workorders
Results:
13/07/1992 07/13/1992 13-07-1992 07-13-1992
07/02/1995 02/07/1995 07-02-1995 02-07-1995
20/07/1993 07/20/1993 20-07-1993 07-20-1993
Note that the values stored in the DateTime column in the database are
not changed. The date is formatted from a DateTime data type to a string format using the format string in C# or the
Convert
function in SQL.