473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 4151
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

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

Similar topics

13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
2
by: Jennifer Lee | last post by:
Hello, Does anyone happen to have a function, prefereably in pl/pgsql, which given the month and year will return the last day in that month? Thanks, Jennifer ...
3
by: Gol Yass | last post by:
Dear Newsgroup, Is there a function in Access XP to take a date or month , and return the last day in that month? Ex: the last day in september is 30 or the last day in May is 31 . Thank...
13
by: Sue | last post by:
I'm working on a database that keeps track of employees hired by a general contractor on a project by project basis. These employees are hired to work on a project and are then laid off either at...
2
by: Sasidhar Parvatham | last post by:
Hello All, Is there a way I can get dates for first and last fridays of a month? Any suggestion would be helpful. Thanks.
4
by: laredotornado | last post by:
Hi, Using PHP 4, if I have a date, what is a function I could use to give me a date that represents the first day of that month? For example, if my date were "3/19/2006 8:00", I would want my...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
3
by: ats | last post by:
Does anybody have any sample code for calculating the date for teh last Friday in each month. TIA -- ats@jbex When an old lady got hit by a truck I saw the wicked gleam in your eyes
2
code green
by: code green | last post by:
On the first of each month I need to create a report from the previous month covering 1-15 and 16-end of month. I am using the following code which forms part of a mssql query and part of the...
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.