Two-character month and day in Expression Builder

I needed to set a variable to YYYYMMDD.

When using the Month() and Day() function in Expression Builder, it returned a single character back when the value was less than 10. So the value I was getting back was YYYYMD using the following expression:

(DT_WSTR, 4) YEAR( GETDATE()  ) + (DT_WSTR,2)DatePart("m", getdate()) + (DT_WSTR,2)DatePart("d", getdate())

The workaround is to use the RIGHT() function:

(DT_WSTR, 4) YEAR( GETDATE()  ) + RIGHT("0" + (DT_WSTR,2)DatePart("m", getdate()), 2) + RIGHT("0" + (DT_WSTR,2)DatePart("d", getdate()), 2)

The above will return the right format: YYYYMMDD.

Leave a Reply

Spam Protection by WP-SpamFree

RedGate SQL-Compare
RedGate SQL-Compare
RedGate Data Compare
RedGate Data Compare
Quest Spotlight
Quest Spotlight
Quest Performance Analysis
Quest Performance Analysis