Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « DATEFROMPARTS and DATETIMEFROMPARTS functions in SQL Server Denali CTP3A Quick look at the new IIF function in Denali CTP3 »
    comments

    SQL Server Denali CTP3 has a bunch of new date/time functions like DATEFROMPARTS, DATETIMEFROMPARTS and EOMONTH

    First let's take a look at EOMONTH.

    The syntax for EOMONTH is

    EOMONTH ( start_date [, month_to_add ] )

    If you pass in getdate() you will get the last day of the month for the current month

    1. SELECT EOMONTH(getdate())

    2011-07-31 00:00:00.000

    If you pass in a date, you will also get the last date for that month

    1. SELECT EOMONTH('20110615')

    2011-06-30 00:00:00.0000000

    This function also accepts an optional parameter: month_to_add

    month_to_add
    Optional integer expression specifying the number of months to add to start_date.

    If this argument is specified, then EOMONTH adds the specified number of months to start_date, and then returns the last day of the month for the resulting date. If this addition overflows the valid range of dates, then an error is raised.

    So if we pass 1 for month_to_add it will add a month

    1. SELECT EOMONTH('20110615',1)

    2011-07-31 00:00:00.0000000

    If we pass -1 for month_to_add it will subtract a month

    1. SELECT EOMONTH('20110615',-1)

    2011-05-31 00:00:00.0000000

    The one problem with this function is that if you do a query and specify between some date and EOMONTH it won't give you anything after midnight. I already explained that in this post: How Does Between Work With Dates In SQL Server?

    I also wonder why there is no SOMONTH function? Yes, I know it starts with 1, but if there is an end of month function then someone will also search for a start of month function.

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    913 views
    Instapaper

    5 comments

    Arthur Awesome on one hand - a productivity improvement, on the other, the developers become a more lazy bunch.
    07/13/11 @ 11:32
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) Why call it EOMONTH? What was wrong with ENDOFMONTH?
    07/13/11 @ 12:51
    Comment from: SQLDenis [Member] Email
    SQLDenis EOMONTH has the same name in Excel, this way Excel users will be able to use the same functions in SQL Server
    07/13/11 @ 12:58
    Comment from: Erik [Member] Email
    Erik SOMONTH = EOMONTH(Date, -1) + 1

    of course +1 won't work unless it's datetime.
    07/13/11 @ 13:30
    Comment from: SQLDenis [Member] Email
    SQLDenis Or plain vanilla

    SELECT DATEADD(mm, DATEDIFF(mm, 0, getdate()), 0) AS FirstDayOfMonth,
    DATEADD(mm, DATEDIFF(mm, 0, getdate())+1, -1) AS LastDayOfMonth

    See also here: How to find the first and last days in years, months etc
    07/13/11 @ 13:34

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)