Introduction:
Description:
In previous articles I explained Insert values in identity column in SQL Server, Check if string contains specific word in SQL, SQL Query to read xml file, SQL Query to get duplicate records count, SQL Query to get month wise, year wise data and many articles relating to SQL Server, jQuery, JavaScript. Now I will explain how to get or find total number of days in month using SQL Server.
In previous articles I explained Insert values in identity column in SQL Server, Check if string contains specific word in SQL, SQL Query to read xml file, SQL Query to get duplicate records count, SQL Query to get month wise, year wise data and many articles relating to SQL Server, jQuery, JavaScript. Now I will explain how to get or find total number of days in month using SQL Server.
To
get total number of days in a month we need to write the query like as shown
below
DECLARE @sample datetime
SET @sample=GETDATE() -- Sample date
select datediff(dd,dateadd(dd, 1-day(@sample),@sample), dateadd(m,1,dateadd(dd, 1-day(@sample),@sample)))
|
By using above query we can get total number of days
for particular month.
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
12 comments :
hah/...Good One.. :)
It is not working as expected.When I run this query I'm getting output:28 where expected result:30.As the current month is APRIL and it has 30days.
Hope I'm not wrong.
query not working properly
Try this:
DECLARE @sample datetime
SET @sample=GETDATE() -- Sample date
SELECT DAY(DATEADD(mm,1, DATEADD(dd, - Day(@sample),@sample)))
ya good
The query is working absolutely fine.
Thanks to the guy who has shared this information.
Good 1 thanks
jhhj
we can use this also.
declare @i int,@d1 datetime,@d2 datetime
select @i=day('2/12/2013')-1
select @d1= dateadd(dd,-@i,'2/12/2013')
select @d2=dateadd(mm,1,@d1)
select datediff(dd,@d1,@d2)
declare @Date datetime,@days INT
SELECT @Date=GETDATE(),@days =(day(@date)-1)
SELECT DATEDIFF(DD,DATEADD(DD,-@DAYS,@DATE),DATEADD(MM,1,DATEADD(DD,-@DAYS,@DATE)))
Very Nice Code. Its really save much time to do logic in c#
Note: Only a member of this blog may post a comment.