-4

Below is the image of my code please have a look on it.

I am trying to retrieve data from excel sheet and storing it into database table through SQL bulkcopy.

Error:

The date format is 05-01-2019; it is inserted as 2019-05-01 (database) incorrectly - correct date is 2019-01-05.

When date is greater than 12 it stores in correct format.

2019-12-25 (database) correct 
Excel : 25-12-2019

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 8
    You've not posted an image, however, before you do, **don't**. Images of data, code, etc, use all but useless to the volunteers you're asking help from. Post data/code as what is it: `text`. – Thom A Jan 15 '19 at 10:37
  • Possible duplicate of [Converting a String to DateTime](https://stackoverflow.com/questions/919244/converting-a-string-to-datetime) – VDWWD Jan 15 '19 at 10:38

1 Answers1

0

convert your string to a date first with

DateTime.ParseExact("25-12-1986", "dd-MM-yyyy", NULL)

then format it into your date

you can also consider TryParseExact to check for wrong format

your problem is that 05-01-2019 tends to mean 'May 1st 2019' in US style date formats, therefore you need to be very careful with formats. The policy of automatically making 25-12-1966 into 25th December (which is all it could be interpreted as) is not as helpful as it seems.

Cato
  • 3,652
  • 9
  • 12