Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime from excel format into the sql format

Tags:

c#

.net

I'm reading values from Excel and inserting read rows to SQL, but I'm facing an issue with date columns.

From Excel I'm reading the dates as 23-08-2011 01:33:01, but in the database the values are stored as 2011-08-22 10:21:18.000.

Is there a way I can convert in C# (not in T-SQL) the date I'm reading to a yyyy-mm-dd 00:00:00:000 format? I want to convert 23-08-2011 01:33:01 to 2011-08-23 00:00:00:000.

like image 315
NoviceToProgramming Avatar asked Nov 27 '25 06:11

NoviceToProgramming


1 Answers

  1. Parse Excel string into DateTime object using DateTime.ParseExact
  2. Format DateTime object into SQL format using String.Format and the appropriate format string.

See these two resources for guide how to create a DateTime format string:

Standard Date and Time Format Strings

Custom Date and Time Format Strings

DateTime excel = DateTime.ParseExact("23-08-2011 01:33:01", "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture);
String sqlString = String.Format("{0:yyyy-MM-dd hh:mm:ss:ffff}", excel);
like image 79
Nemanja Boric Avatar answered Nov 28 '25 19:11

Nemanja Boric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!