Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How do I get Date in SQL Server?

Author  Topic 

amgrace
Starting Member

30 Posts

Posted - 2004-06-03 : 19:21:01
Hello again everyone,

The code below gives me the Start date of 3 months ago (march):

select dateadd(mm,-3,dateadd(mm,datediff(mm,0,getdate()),0))

QUERY OUTPUT:
2004-03-01 00:00:00.000

Which query will give me the End date of 3 months ago i.e 2004-03-31

I need to use this info to construct my query that analyses data in my database 3 months in arrears i.e This is June, but I am analysing the data for March.

Thank you

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-06-03 : 19:31:17
select dateadd(dd,-1,dateadd(mm,-2,dateadd(mm,datediff(mm,0,getdate()),0)))


MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-06-03 : 20:08:59
If you used 3/31/2004 as your end date, then you are missing everything that happend on 3/31. You want your start date and end date to be "midnight"

If startdate is 3/1/2004, it's really 3/1/2004 00:00:00.
So, 3/31/2004 would be 3/31/2004 00:00:00 the START of 3/31 and not the END of 3/31. You might want to do a DATEADD(dd, 1, @EndDate) to make sure you get the entire day's worth of data, or change your enddate to 4/1/2004 00:00:00, the End of 3/31.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-06-04 : 04:11:01
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=35801

hmmm, no patience some people...
Go to Top of Page

amgrace
Starting Member

30 Posts

Posted - 2004-06-04 : 08:02:23
Thanks very much Rick....the code:

select dateadd(mm,-2,dateadd(ms,-3,dateadd(mm,datediff(mm,0,getdate()),0)))


gave the the correct output:

2004-03-31 23:59:59.997

THanks a lot to all you guys who replied as well....
Go to Top of Page
   

- Advertisement -