简体   繁体   中英

DB2 C# Date Conversion

I am migrating about 60 tables of data from DB2 to SQL server. The problem I have is with Date (I use that work lightly) fields in the DB2 tables. The tables have approximately 15 years worth of data in them. Over the time there have been dates stored in most tables in either Data type fields or in string (Char) type fields. Some of these table contain in excess of 1 milllion records.

Add to this the dates have been stored in various formats

Here are a few.

23.06.2005
06.23.2005
2005.06.23
23.06.05
06.23.05

23/06/2005
06/23/2005
2005/06/23
23/06/05
06/23/05

23-06-2005
06-23-2005
2005-06-23
23-06-05
06-23-05

When the DB2 field is a Data Type field I have no problem.

DateTime dt = DateTime.Parse(dsSourceData[j].ToString(), new CultureInfo("en-CA"));

I do however have a problem when the DB2 field is a String type. The above conversion fails.

Has anyone any idea how I may overcome this problem?

I would appreciate and assistance offered

Kind regards

Iain

It's impossible to provide a working solution because you have two contradictory formats where the day and month is on interchanging positions: "23.06.05" and "06.23.05". So what does this date for example mean: "07.08.09" ?

However, normally you use DateTime.TryParseExact with multiple format-string for this:

string[] formats={"dd.MM.yyyy", "yyyy.MM.dd", "dd.MM.yy"}; // to be continued
CultureInfo culture = new CultureInfo("en-CA");
var validDates = dateStrings
    .Select(s => (valid:DateTime.TryParseExact(s, formats, culture, DateTimeStyles.None, out var dt),date:dt))
    .Where(x => x.valid)
    .Select(x => x.date)
    .ToArray();

Read: Custom date and time format strings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM