C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
Today
    Last 7 Days more...






    Resources » Articles » Databases »

    How to find the number of days in a month


    Posted Date: 03 Jan 2007    Resource Type: Articles    Category: Databases
    Author: Vadivel MohanakrishnanMember Level: Diamond    
    Rating: 1 out of 5Points: 10



    Introduction



    This seems to be one another frequently asked question in the discussion forums. So thought would write a small article on this today.

    Basic Solution



    With the help of built in SQL Server functions we can easily achieve this in one single T-SQL statement as shown below.

    Select Day(DateAdd(Month, 1, '01/01/2007') - Day(DateAdd(Month, 1, '02/01/2007')))

    Generalized Solution:



    We can generalize it by creating a "User defined Stored Procedure" as shown below:



    Create Function NumDaysInMonth (@dtDate datetime) returns int
    as
    Begin
    Return
    (
    Select Day(DateAdd(Month, 1, @dtDate) - Day(DateAdd(Month, 1, @dtDate)))
    )
    End
    Go



    Test:





    Select dbo.NumDaysInMonth('20070201')
    Go






    Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    (No tags found.)

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Find tables which doesn't have Primary Key ...
    Previous Resource: List tables that doesn't participate in any relationships ...
    Return to Discussion Resource Index
    Post New Resource
    Category: Databases


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use