loading..
Ðóññêèé    English
12:47

EOMONTH function

How do we determine the last day of a month?

We can determine The current date using CURRENT_TIMESTAMP function:

Console
Execute
  1. SELECT current_timestamp;

To find out the last day of the previous month, we can subtract day number from current date, i.e. the number of days from beginning of the month:

Console
Execute
  1. SELECT dateadd(dd, -day(current_timestamp), current_timestamp);

Now we need to add one month to current date beforehand:

Console
Execute
  1. SELECT dateadd(dd, -day(dateadd(mm, 1, current_timestamp)),
  2. dateadd(mm, 1, current_timestamp));

Lastly, let's eliminate a time component from the result obtained above:

Console
Execute
  1. SELECT CAST(
  2. dateadd(dd, -day(dateadd(mm, 1, current_timestamp)), dateadd(mm, 1, current_timestamp))
  3. AS date);

Since  A database management system (DBMS) by Microsoft Corporation. SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Server 2012, EOMONTH function is available. This function allows us to obtain the same result without "procedural" logic:

Console
Execute
  1. SELECT CAST(
  2. dateadd(dd, -day(dateadd(mm, 1, current_timestamp)),
  3. dateadd(mm, 1, current_timestamp)) AS date
  4. ) old_way, eomonth(current_timestamp) new_way;

If an error occurs when running above query, this means that tutorial is working with SQL Server version under 2012, and EOMONTH function is not supported.

Well, when I wrote this query, the following result was obtained

old_way    new_way
2016-07-31    2016-07-31

As we could see, EOMONTH function has date-type parameter. Moreover, there is second optional parameter which presents the number of months to be added, if any, to the first parameter. For example, the following query gives the latest days of previous month, current month and next month for the date '2016-01-28' respectively:

Console
Execute
  1. SELECT eomonth('2016-01-28',-1) prev_month,
  2.                   eomonth('2016-01-28') this_month,
  3.                   eomonth('2016-01-28', 1) next_month;

prev_month    this_month    next_month
2015-12-31    2016-01-31    2016-02-29

Bookmark and Share
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.