Monday, February 20, 2012

Is there any easy way to evaluate complex date logic in expressions?

Hello all,

I am new to SSIS, so I am hoping there is an easier way to do this...

I need to evaluate a date in a field and determine if it is between the beginning and end of whatever the current month is... In Access, this was written as something like:

IIF(datevalue >= CDate(Format(Now(),"mm/01/yy")) AND datevalue < CDate(Format(DateAdd("m",1,Now()), "mm/01/yy)), value1, value2)

Trying to recreate this in SSIS using expressions during a derived transformation has been extremely difficult. Here is what I came up with:

(DUE_DATE >= (DT_DATE)( (DT_WSTR,2)MONTH(GETDATE())+"/01/"+ (DT_WSTR,2)YEAR(GETDATE()))) && (DUE_DATE<(DT_DATE)( (DT_WSTR,2)MONTH( DATEADD("m",1,GETDATE()) )+"/01/"+(DT_WSTR,2)YEAR( DATEADD("m",1,GETDATE() )))) ? value1 : value2

Any help you all could give would be appreciated.

Thanks!

Josh

I've just given this a go with a column called [OrderDate] (which comes from a SalesOrderHeader in AdventureWorks). The following works fine for me:

OrderDate < GETDATE() ? "Y" : "N"

Can you not do that? Why are you trying to parse out the different parts of GETDATE()?

-Jamie

|||This expression will determine if the DUE_DATE is in the current month, as defined by GETDATE().

YEAR(GETDATE()) + MONTH(GETDATE()) == YEAR(DUE_DATE) + MONTH(DUE_DATE) ? "value1" : "value2"

|||

jaegd wrote:

This expression will determine if the DUE_DATE is in the current month, as defined by GETDATE().

YEAR(GETDATE()) + MONTH(GETDATE()) == YEAR(DUE_DATE) + MONTH(DUE_DATE) ? "value1" : "value2"

I would suggest using :

(YEAR(GETDATE())*100) + MONTH(GETDATE()) == (YEAR(DUE_DATE)*100) + MONTH(DUE_DATE) ? "value1" : "value2"

That will result in 200612 for Dec 2006.

|||

Hi Jaimie,

Thanks for the advice. However, I need to be able to tell if the due_date is between the 1st and last day of the currernt (or any given) month. I think your suggestion would return true for all orders with a due_date less than today... which would exclude orders due in the remainder of the month and would include orders due prior to the beginning of the month.

Best regards,

Joshua

|||

Hi Tom,

Thanks for the help! I think that will do what I'm looking for and will be a lot easier...

Best regards,

Joshua

|||

jrbarker33 wrote:

Hi Jaimie,

Thanks for the advice. However, I need to be able to tell if the due_date is between the 1st and last day of the currernt (or any given) month. I think your suggestion would return true for all orders with a due_date less than today... which would exclude orders due in the remainder of the month and would include orders due prior to the beginning of the month.

Best regards,

Joshua

Whoops. Sorry. I need to read things more accurately!

Tom's code is definately the way to go then.

-Jamie

No comments:

Post a Comment