1

I have a fixed format .txt file which has a date field. This field may have a date or be blank. I have wracked my brains to get this field to convert from DT_STR to DT_DBDATE in the Derived Column transformation. I have tried multiple scenarios:

[CHNG_DT] == "" ?  ISNULL((DT_DBDATE)[CHNG_DT] ) : (DT_DBDATE)SUBSTRING([CHNG_DT], 1, 4)) + "-" + (DB_DBDATE)(SUBSTRING(CHNG_DT], 6, 2)) + "-" + (DB_DBDATE)(SUBSTRING([CHNG_DT], 9, 2))

[CHNG_DT] == "" ? NULL(DB_DBDATE) : (DB_DBDATE)[CHNG_DT]

ISNULL([CHNG_DT]) ? NULL(DT_DBDATE) : (DT_DBDATE)((DT_STR, 10, 1252)[CHNG_DT])

ISNULL(CHNG_DT) ? NULL(DT_DBDATE) : (DT_DBDATE)((DT_STR,10,1252)CHNG_DT)

LEFT([CHNG_DT], 10) == "          " ? ISNULL((DT_DBDATE)[CHNG_DT]) : (DT_DBDATE)[CHNG_DT]

(DT_DBDATE)CHNG_DT
Hadi
  • 36,233
  • 13
  • 65
  • 124
Brad G.
  • 11
  • 1
  • 2

1 Answers1

0

First of all, when you are looking to convert values from DT_STR to DT_DBDATE, you should make sure that those values are stored in the yyyy-MM-dd format. You can refer to the following official documentation to learn more:

The second suggestion is to use the LTRIM() and RTRIM() functions to make sure that there are no white blanks in the beginning and end of the date values. As an example:

LTRIM(RTRIM(REPLACENULL([CHNG_DT],""))) == "" ? NULL(DT_DBDATE) : (DT_DBDATE)LTRIM(RTRIM([CHNG_DT]))

If date values are stored in a different format, you should write an expression that changes the format before converting it to DT_DBDATE. As an example:

Other helpful answers:

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • The date in the file is formatted yyyy-mm-dd or it can be blank. Either way, it is the same 10 positions within the file. If it is blank, then I need to convert it to NULL to store in the table. – Brad G. Jan 11 '21 at 15:48
  • We determined that part of the issue here was the field definition on the table, CMBL_CHDT date not null. I changed this to CMBL_CHDT null, and was then able to use CHNG_DT == " " ? NULL(DT_DBDATE) : (DT_DBDATE)CHNG_DT to get the dates loaded correctly. – Brad G. Jan 12 '21 at 13:33