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
 SQL Server Development (2000)
 Is there a better way

Author  Topic 

humanpuck
Yak Posting Veteran

94 Posts

Posted - 2006-08-10 : 13:27:40
To get the first and last day of the last month?

comm_date >= dateadd(dd,-datepart(dd,dateadd(mm,-1,dateadd(day,datediff(day,0,GETDATE()), 0))),
dateadd(mm,-1,dateadd(day,datediff(day,0,GETDATE()), 0)))+1 and
comm_date <= dateadd(dd,-datepart(dd,dateadd(mm,-1,dateadd(day,datediff(day,0,GETDATE()), 0))),
dateadd(day,datediff(day,0,GETDATE()), 0))

X002548
Not Just a Number

15586 Posts

Posted - 2006-08-10 : 13:35:51
Have a read here

http://weblogs.sqlteam.com/brettk/archive/2005/06/02/5528.aspx



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-08-10 : 14:22:26
quote:
Originally posted by humanpuck

To get the first and last day of the last month?

comm_date >= dateadd(dd,-datepart(dd,dateadd(mm,-1,dateadd(day,datediff(day,0,GETDATE()), 0))),
dateadd(mm,-1,dateadd(day,datediff(day,0,GETDATE()), 0)))+1 and
comm_date <= dateadd(dd,-datepart(dd,dateadd(mm,-1,dateadd(day,datediff(day,0,GETDATE()), 0))),
dateadd(day,datediff(day,0,GETDATE()), 0))



Seems like the hard way to do it.

This code is a little simpler:
select 
comm_date >= dateadd(mm,datediff(mm,0,getdate())-1,0) and
comm_date <= dateadd(mm,datediff(mm,-1,getdate())-1,-1)



You may want to look at these links.

Start of Time Period Functions:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64755

Start of Week Function:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47307

End Date of Time Period Functions:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64759

End of Week Function:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64760



CODO ERGO SUM
Go to Top of Page
   

- Advertisement -