Find the first Day (01) and the Last Day (28) for a specified month in SQL Function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fahhad
    New Member
    • Feb 2013
    • 4

    Find the first Day (01) and the Last Day (28) for a specified month in SQL Function?

    I need to write an SQL Function (UDF) to find the First Day (int) and the Last Day (int) when I specify a date.

    For example: When I specify (20130217) (i.e. Feb 17th, 2013), I should be able to find the First Day (01) and the Last Day (28) for the specified month (in this case, Feb).

    How can I do that? Please provide the necessary code to implement this feature in my SQL Function.

    Thanks.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    This is not a code writing service but if you post the code you've tried along with a description of the problem you're having with the code, we can help guide you to a solution.

    As a hint, you can use the various date functions combined with cast functions to build the dates you need.

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      I think he's just asking for a straight forward function to get the first and last day of the month.


      Here, find your dates here.

      Happy Coding!!!


      ~~ CK

      Comment

      • swathee
        New Member
        • Dec 2013
        • 9

        #4
        Code:
        DECLARE @yourDate DATETIME
        SET @yourDate = '2013-02-17'
        
        SELECT DAY(DATEADD(MONTH, DATEDIFF(MONTH, 0, @yourDate), 0)) AS First,
               DAY(DATEADD(MONTH, DATEDIFF(MONTH, -1, @yourDate), -1)) AS Last
        Last edited by Rabbit; Dec 9 '13, 05:13 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

        Comment

        Working...