You must Sign In to post a response.
  • Category: SQL Server

    To get the number of days in a certain month in a specified range

    I need to get the number of days in a certain month in a specified range

    I have a start date & end date
    For eg , start date = 1/8/2016
    end date = 31/12/2016.I need to get the no . of days allocated in september.

    How to find that?
  • #768042

    We can simply get those days range. You have not specified you have this requirements in .net or Sql
    Let me post you for both.


    We can subtract the days from and to and multiple with the days.
    FromDate = 1-9-2016
    ToDate = 30-9-2016

    So you can perform,

    DateTime FromDate;
    DateTime ToDate;

    return (ToDate- FromDate)


    In Sql it will be much simpler because we have predefined function for those.

    declare @FromDate datetime
    declare @ToDate datetime

    set @FromDate = '01/09/2016'
    set @ToDate = '30/09/2016'

    select DATEDIFF(d, @FromDate , @ToDate )

    Value= 30

    Kindly let us know is this what you are expecting. Or something else


  • #768066
    Hai Anu,
    There is a inbuilt function called as DATEDIFF in Sql Server which can be used to get the difference in between dates by passing the 'from date' and 'To date'as the input parameters.

    Select DATEDIFF (d, @fromDt , @toDt))

    We can also get the difference between dates in Weeks, Months, years etc.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #768073
    hi Anu,
    yes we can simply find between two months values in day formation. with the use of date function months between. I will explain 1 example hope you can understand easily.

    example: find the day value between Issue_date and submission_day from issue_detail table

    select book_code,issue_date,submission_date,
    months_between(submission_date,issue_date)*31 "total days"
    from issue_detail;

    run this query in your pc and see the result you can understand cleary.
    hope this help you

    thank you

Sign In to post your comments