I have a Datatable with two DateTime columns: date_from and date_to.
string dateFrom = row["date_from"].ToString();
string dateTo = row["date_to"].ToString();
When this code is run in a customer environment, it returns dates in the following format:
"2016-01-01 00.00.00"
"2016-02-01 00.00.00"
This string is then inserted into SQL:
SELECT * FROM my_table
WHERE UPPER(status) = 'N'
AND trans_date >= {ts '1900-01-01 00.00.00' }
AND trans_date <= {ts '1900-01-01 00.00.00' }
When this SQL is executed it returns the error "Conversion failed when converting date and/or time from character string."
So a quick fix for this is to run a string.Replace() where I replace period with colon:
dateFrom = dateFrom.Replace(".", ":");
However, my question is rather why the date is returned with periods as the timestamp separator and not colons?
I have created this test on my local machine, but unfortunately for this scenario it returns a correct representation of the DateTime string:
DataTable table = new DataTable();
table.Columns.Add("date", typeof(DateTime));
DataRow row = table.NewRow();
row["date"] = DateTime.Now;
table.Rows.Add(row);
DateTime date = DateTime.Parse(table.Rows[0]["date"].ToString());
My guess so far would be that it has something to do with the customers culture, but I would really appreciate input from someone who has experience with this so I can give feedback to the customer about why this happened in the first place.
It might just be a coincidence, but it is worth mentioning that this only happens for users with Windows 10. Everyone using Windows 7 does not get this error.