SSIS Expression How to get correct date minus two days

I am trying to get the date for two days ago.  It works with the following, except for when a new month begins.

(DT_WSTR,4)DATEPART("yyyy",GetDate()) +
 RIGHT("0" + (DT_WSTR,2)DATEPART("mm",GetDate()) ,2) +
 RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -2, GETDATE())),2)

Today it yields 20180231, but it should yield 20180130.
Thank you for your help with this.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
you will need to do the getdate() - 2 wherever you are using getdate().

why not simplify as Cast(getdate() - 2 as date) or use format(getdate() - 2,'yyyyMMdd')
newtoperlpgmAuthor Commented:
I should have clarified I am using it in SSIS as an SSIS expression.  I will try your suggestion.  I hope it works.!  Thank you.
Mark WillsTopic AdvisorCommented:
Worst case is to make sure you use the first suggestion. Where ever you have getdate() make sure it is getdate() - 2

EDIT: in SSIS parlance that is DATEADD("day",-2,GETDATE())
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

alpa buddhabhattiCommented:
I think you need to use something like DATEADD("day", -2, GETDATE()).

or ..
you can create 2 seprate variable one for start date and one for number of difference in day..
DATEADD( "dd", @[User::DaysBack] , @[User::CurrentDate])

Or alternatively you could enter:

DATEADD( "dd", @[User::DaysBack] , GETDATE())
newtoperlpgmAuthor Commented:
I will give your suggestion a try.
newtoperlpgmAuthor Commented:
that syntax did not work.
Mark WillsTopic AdvisorCommented:
when I said getdate() - 2, it was in reference to what you are already doing for DATEPART("DD"....

(DT_WSTR,4)DATEPART("yyyy",DATEADD("day", -2, GETDATE())) +
 RIGHT("0" + (DT_WSTR,2)DATEPART("mm",DATEADD("day", -2, GETDATE())) ,2) +
 RIGHT("0" + (DT_WSTR, 2)DATEPART("DD", DATEADD("day", -2, GETDATE())),2)


DATEPART("yyyy", DATEADD( "day",-2, GETDATE()))*10000 + DATEPART("month",  DATEADD( "day",-2, GETDATE())) * 100 + DATEPART("day", DATEADD( "day",-2, GETDATE()))

I think your padding is needed when using YEAR() MONTH() DAY() functions

(DT_WSTR,4)YEAR(DATEADD("day", -2, GETDATE())) +
 RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("day", -2, GETDATE())) ,2) +
 RIGHT("0" + (DT_WSTR, 2)DAY(DATEADD("day", -2, GETDATE())),2)

But it has been a while :)
And my shorthand speak is more T-SQL oriented
Apologies for any confusion.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.