user2431727 user2431727 - 1 year ago 77
SQL Question

Insert string value to datetime column

I have two columns named exam_date (string nvarchar) and submitted_date(Date Time). The exam_date will always be "dd-MM-yyyy" format.Now I want to insert exam_date to submitted date. How is it possible?
I have tried like below but shows "String was not recognized as a valid DateTime."

string submitted_date="12-04-2016";

Answer Source

You can use DateTime.ParseExact:

DateTime submittedDate = DateTime.ParseExact("12-04-2016", "dd-MM-yyyy", null);

But you should really change the column exam_date to a datetime and not a nvarchar type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download