Monday, April 27

SQL to find first and last day of a month

How to get the first and last day of any month.

To get the first and last day of any month when given a date we can use the date functions
as below.

Suppose we want the first and last day for the date '04/28/2009', that is for April 2009.

declare @given_date datetime
select @given_date = '04/28/2009'

-- SQL to get the first day of the month
select dateadd(dd,-(day(dateadd(mm,1,@given_date))-1),dateadd(mm,0,@given_date))

-- SQL to get the last day of the month
select dateadd(dd, -day(dateadd(mm,1,@given_date)), dateadd(mm,1,@given_date))

To get next or previous month's first or last day change the highlighted number accordingly.



--SQL to get the first day of the second next month
select dateadd(dd,-(day(dateadd(mm,1,@given_date))-1),dateadd(mm,2,@given_date))

-- SQL to get the last day of the second next month
select dateadd(dd, -day(dateadd(mm,1,@given_date)), dateadd(mm,3,@given_date))

2 comments:

Modern Sport Watches said...

Sport watches mostly represents the modern wrist watches, but it can also be used casio watches represent the young age for boys and girls
watches.When you first begin shopping for watches, i recommend g shock and nice casio baby g watches.Shop over thousands of styles fine watches
including many Casio watches,casio pathfinder, g shock, waveceptor.

Layalina Hookah Tobacco Smooth Flavor said...

Known widely across the Middle East for its exceptionally smooth flavor and thick smoke, LayLayalina is the newest addition to our shisha tobacco selection. alina will be sure to tickle your taste buds!

http://www.maharajahookah.com/layalina-hookah-tobacco/cat_19.html
http://www.maharajahookah.com/layalina-hookah-tobacco/golden-layalina-california-dream-flavor/prod_365.html

Layalina Hookah Tobacco
Golden Layalina California Dream Flavor





Disclaimer :
Unless, otherwise mentioned, TF's SQL tips/tricks are for Sybase ASE 12.5.x