SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to find first and last day of the month
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ms.gangadhar
Starting Member

India
8 Posts

Posted - 06/05/2012 :  03:05:17  Show Profile  Reply with Quote
Hi,

I order to run a report for a particular month i need to fidn the first and last day of the month.How can we do this.

Regards,
Gangadhara

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 06/05/2012 :  03:13:14  Show Profile  Visit webfred's Homepage  Reply with Quote
-- -----------------------------------------
-- get 1. day of actual month
-- -----------------------------------------
Select DateAdd(Month,DateDiff(Month,0,getdate()),0)

-- ----------------------------------------------
-- get LAST day of actual month
-- ----------------------------------------------
Select DateAdd(dd,-1,DateAdd(Month,DateDiff(Month,0,getdate())+1,0))


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 06/05/2012 :  05:23:05  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
You may also need to look at the code shown at the bottom of this post
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jeffw8713
Aged Yak Warrior

USA
693 Posts

Posted - 06/05/2012 :  14:13:04  Show Profile  Reply with Quote
quote:
Originally posted by webfred

-- -----------------------------------------
-- get 1. day of actual month
-- -----------------------------------------
Select DateAdd(Month,DateDiff(Month,0,getdate()),0)

-- ----------------------------------------------
-- get LAST day of actual month
-- ----------------------------------------------
Select DateAdd(dd,-1,DateAdd(Month,DateDiff(Month,0,getdate())+1,0))


No, you're never too old to Yak'n'Roll if you're too young to die.



For last day of the month, it is simpler to use:

DATEADD(month, DATEDIFF(month, -1, getdate()), -1)

Or, to move forward/backward xx number of months:

DATEADD(month, DATEDIFF(month, -1, getdate()) + xx, -1); -- where xx is the number of months forward/backward
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000