## Calculating the No. of days in a month

### Calculating the No. of days in a month

Anybody know how to calc the number of days in a month, given a date ??

I can do it in Access/vb easily, but can't figure out how to do it in TSQL.
Forgive me if it's too easy - I'm new here...

Thanks
Darren

### Calculating the No. of days in a month

Darren
Look at this one

Quote:> Anybody know how to calc the number of days in a month, given a date ??

> I can do it in Access/vb easily, but can't figure out how to do it in
TSQL.
> Forgive me if it's too easy - I'm new here...

> Thanks
> Darren

### Calculating the No. of days in a month

Thanks Uri
It's just too easy when you know how...
Darren

### Calculating the No. of days in a month

instead of SQL you are saying

"What day of the month is (
The current date/time plus 1 month
minus
What day of the month is
The current date/time plus 1 month
)"

That is, today being 24/10/2002 in British, you are taking
24/11/2002 and then subtracting 24 to get the last date this
month, and then taking the day-of-month of that.  And it
doesn't matter that DATEADD(m,1,'2002-10-31') is '2002-11-30'
because in that case you get the same difference each time as
you subtract 30 from '2002-11-30'.  (Obviously it /would/
but it isn't.)

Having said that, how do we feel about

SELECT CONVERT( int, DATEADD(m,1,GETDATE()) - GETDATE() ) ? ;-)

(Oh, and what if you start to execute either one at
'2002-10-31 23:59:59.99'... ;-)

> Darren
> Look at this one

> > Anybody know how to calc the number of days in a month, given a date ??

> > I can do it in Access/vb easily, but can't figure out how to do it in
>  TSQL.
> > Forgive me if it's too easy - I'm new here...

> > Thanks
> > Darren

I searched all over google for answers to this question and came across many
solutions. But converting dates to character and plugging a "01" in and
putting the date back together seemed to old school.

Ex..

Or maybe it had a different purpose that I didn't catch in the thread. But
tinkering with Healthcare data has caused me to tinker with dates more than
I could care for.

I figure I would just post this incase someone else needed a simple solution
that didn't eat up the runtime of the query.

/*CurrentMonth is set to the first day of the month*/

/*Figure out the last day of the current month */

/*Figure out the last day of the previous month */

Not to mention storing all of these little bits of code in the newsgroup
allows me to find them in the future from anywhere.