Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get number of days in a month when i pass

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2014-02-04 : 12:04:19
Is it possible to get number of days in a month when i pass a date. like yyyy.mm.dd as 2012.01.01

showing result as 31.

Thanks a lot for the helpful info.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-02-04 : 13:16:10
Here is one way:
DECLARE @Foo DATETIME = '20120101'

SELECT DAY(DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @Foo) + 1, 0)))
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2014-02-04 : 13:33:58
[code]

DECLARE @your_date datetime
SET @your_date = '2012.01.01'

SELECT DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, @your_date), 0), DATEADD(MONTH, DATEDIFF(MONTH, 0, @your_date) + 1, 0))

[/code]
Go to Top of Page
   

- Advertisement -