Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given a date (say 30-Jan- 2000), I need to find the first and last date of that year, then the first and last date of the month, and the first and last date of that week. How do i do this?
Posted

1 solution

Hi For Start and end of the year
SQL
select dateadd(d,-datepart(dy,getdate())+1,getdate()) as yearstart,
dateadd(d,-datepart(d,getdate()),dateadd(m,13-datepart(m,getdate()),getdate())) as yearend


For Start and end of the month
SQL
select dateadd(day,-(day('2012-10-01')-1),'2012-10-01')
select dateadd(day,-1,dateadd(month,1,dateadd(day,-(day('2012-10-01')-1),'2012-10-01')))


For first day nad last day of week

SQL
select DATEADD(DAY, 1- DATEPART(DW, '2012-10-12'), '2012-10-12')
SELECT  DATEADD(DAY, 7- DATEPART(DW, '2012-10-12'), '2012-10-12')
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900