469,138 Members | 1,383 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,138 developers. It's quick & easy.

Find out last day of the month

13
Hi,
does anyone have any idea how can we get last date of the month in SQL reportging.
I am using following fomula, but it works only for 31 days of month, not for feb,april,june...(30 days)

=month(today())& "/" & DatePart("d", DateAdd("d", -1, DateAdd("m", 1, DateAdd("d", -DatePart("d", today()) + 1, today())))) & "/" & year(today())
May 8 '07 #1
3 4036
cyberdwarf
218 Expert 100+
Hi

Using T-SQL, try this:-
Expand|Select|Wrap|Line Numbers
  1. [size=1]
  2. declare @d datetime
  3. declare @mth tinyint
  4. set @mth = datepart(mm,getdate())+1
  5. if @mth=13 set @mth = 1
  6. set @d = dateadd(d,-1,convert(datetime,cast(@mth as nvarchar(2)) +'/01/2007',101))
  7.  
HTH
Steve
May 8 '07 #2
jai80
30
Hi,

try this ...

declare @year int,@month int
declare @FirstDate datetime, @LastDate datetime
declare @MonthDays int
set @year=2006
set @month=4
set @FirstDate=convert(datetime, convert(varchar,@month)+'/'+'01'+'/'+ convert(varchar,@year) )
set @MonthDays= DAY(DATEADD(d, -DAY(DATEADD(m,1,@FirstDate)),DATEADD(m,1,@FirstDat e)))
set @LastDate=dateadd(d,@MonthDays-1,@FirstDate)
select @LastDate

GudLuck

Cheers,
jai
May 9 '07 #3
Hi Folks,
Just thought I'd toss this up here.

declare @ld_date datetime
set @ld_date = '2008-11-01'

select dateadd(day, (-1 * datepart(day, dateadd(month, 1, @ld_date))), dateadd(month, 1, @ld_date))

Cheers
Mar 25 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jennifer Lee | last post: by
2 posts views Thread by Sasidhar Parvatham | last post: by
4 posts views Thread by laredotornado | last post: by
3 posts views Thread by ats | last post: by
code green
2 posts views Thread by code green | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.