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
 General SQL Server Forums
 New to SQL Server Programming
 first and last day of the previous month.

Author  Topic 

dsscholar
Starting Member

6 Posts

Posted - 2011-09-22 : 23:44:03
Hi all,

Please help with sql in finding first and last day of the previous month. And does year(datecolumn) and month(datecolumn) give year and month part from a date column?


Thanks in advance.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-09-23 : 00:10:39
[code]
declare @date datetime

select @date = getdate()

select [first day of prev month] = dateadd(month, datediff(month, 0, @date) - 1, 0),
[last day of prev month] = dateadd(month, datediff(month, 0, @date), -1)
[/code]

quote:

does year(datecolumn) and month(datecolumn) give year and month part from a date column?


Yes


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-23 : 00:52:28
Also see

http://visakhm.blogspot.com/2010/01/some-quick-tips-for-date-formating.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-09-23 : 03:54:06
For more such queries, refer this
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-iii.aspx

Madhivanan

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

- Advertisement -