473,394 Members | 1,715 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,394 software developers and data experts.

Simple way to determine the number of days in a month for any given date

DECLARE @varDate datetime
DECLARE @varMonthDate datetime
DECLARE @varYear datetime
DECLARE @varFOM varchar(10)
DECLARE @NumDaysInMonth as int

select @varDate = '2/1/2004'

select @varMonthDate = MONTH(@varDate)
select @varYear = YEAR(@varDate)
select @varFOM = CAST(CAST(@varMonthDate as int) as varchar) + '/1/' +
CAST(CAST(@varYear as int) as varchar)
select @NumDaysInMonth = DATEDIFF (day,@varFOM,DATEADD(mm,1,@varFOM))

select @NumDaysInMonth as DaysInMonth
Jul 20 '05 #1
1 6646
"Matt Karp" <ma******@hotmail.com> wrote in message
news:f2*************************@posting.google.co m...
DECLARE @varDate datetime
DECLARE @varMonthDate datetime
DECLARE @varYear datetime
DECLARE @varFOM varchar(10)
DECLARE @NumDaysInMonth as int

select @varDate = '2/1/2004'

select @varMonthDate = MONTH(@varDate)
select @varYear = YEAR(@varDate)
select @varFOM = CAST(CAST(@varMonthDate as int) as varchar) + '/1/' +
CAST(CAST(@varYear as int) as varchar)
select @NumDaysInMonth = DATEDIFF (day,@varFOM,DATEADD(mm,1,@varFOM))

select @NumDaysInMonth as DaysInMonth


CREATE FUNCTION DaysInMonth (@d DATETIME)
RETURNS INT
AS
BEGIN
RETURN(
SELECT DAY(DATEADD(MONTH, 1, @d) -
DAY(DATEADD(MONTH, 1, @d)))
)
END

SELECT dbo.DaysInMonth('20040201') AS days

days
29

Regards,
jag
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Annette Massie | last post by:
I have a campsite database that tracks the campsite, date arrived and number of days stayed. I need to have statistics showing how many campers were at the campground on any given day or date...
2
by: Mary | last post by:
I am trying to develop a query which will determine the average costs using a rolling average of the past 12 months of data. In other words, if I entered the Ship Month of January and the Ship...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
4
by: Ronald Celis | last post by:
Hi, is there anyway to get the Number of days in a given month and Year in C# thanks Ronald Celis
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
11
by: samuelberthelot | last post by:
Hi, I've got 3 input HTML (dropdown lists) on my page. One for selecting a Month, one for the day, one for the year. Very simple... My problem is that I'd like to update the Days one according...
3
by: LimaCharlie | last post by:
Good Day to Everyone, Need help! I have this SQL table w/ fieldname "PayPeriod". Sample value of PayPeriod are SEP06-1, SEP06-2, OCT06-1, OCT06-2... Wherein the first 3 chars is the month, 06...
6
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future,...
4
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.