How do I select a date 18 months in the past?

I need to build an archive job for SQL 2000, sp4. The mechanics of the actual archiving are pretty straightforward, but I need to limit the archiving to data that older than the 1st day of the month, 18 months ago.

Is there a way within T-SQL to select a month 18 months in the past?
PapaStevensAsked:
Who is Participating?

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

x
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.

chapmandewCommented:
select * from table
where datefield >= dateadd(m, -18, datefield)
0

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
Kevin CrossChief Technology OfficerCommented:
Something like this?
SELECT *
FROM TableName
WHERE DateColumn < DateAdd(mm, -18, DateAdd(dd, -day(getdate())+1, DateDiff(dd, 0, getdate())))

Open in new window

0
PapaStevensAuthor Commented:
These are terrific starts, and I think based on chapmandew's response I have an inkling as to how to proceed. I realize, though, that I've not clarified what I needed as well as I should have - what I need is to select the first day of the month, from the month that is 18 months prior to today's date.

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

chapmandewCommented:
maybe this?


select * from table
where cast(month(datefield) as varchar(2)) + '/1/' + cast(year(datefield) as varchar(4)) >= dateadd(m, -18, cast(month(datefield) as varchar(2)) + '/1/' + cast(year(datefield) as varchar(4)))
0
PapaStevensAuthor Commented:
How would I modify that to only select the date from 18 months ago? In other words, I just need to generate the date, not necessarily select from the table, etc. Apologies for not being clear, it's difficult for me to put into words ;)
0
chapmandewCommented:
try this:

select cast(month(getdate()) as varchar(2)) + '/1/' + cast(year(getdate() as varchar(4))
0
Kevin CrossChief Technology OfficerCommented:
Did my suggestion not work -- http:#22869863?

You said archive, so I gave you all the dates that were less than the first day of the month 18 months ago which is 2007-05-01 00:00:00.000.  If you want to include that day, then that is an easy change.
0
Kevin CrossChief Technology OfficerCommented:
To select just the date as I did it:
SELECT DateAdd(mm, -18, DateAdd(dd, -day(getdate())+1, DateDiff(dd, 0, getdate())))

Open in new window

0
PapaStevensAuthor Commented:
Many thanks, folks - I'll be able to use a combination of solutions from both mwvisa1 and chapmandew, so I'm upping the points and splitting them among both of you. I hope that's okay, and I thank you both for your patience and willingness to work with me ;)
0
Kevin CrossChief Technology OfficerCommented:
Glad to help.  ;)
0
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 2005

From novice to tech pro — start learning today.