Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a solution that gives you the last second of the current month. You can extract the date part or modify it to return just the day. I tested this on SQL Server 2005.</p> <pre><code>select dateadd( s, -1, dateadd( mm, datediff( m, 0, getdate() ) + 1, 0 ) ); </code></pre> <p>To understand how it works we have to look at the <a href="http://www.w3schools.com/sql/func_dateadd.asp" rel="noreferrer">dateadd()</a> and <a href="http://www.w3schools.com/sql/func_datediff.asp" rel="noreferrer">datediff()</a> functions.</p> <pre><code>DATEADD(datepart, number, date) DATEDIFF(datepart, startdate, enddate) </code></pre> <p>If you run just the most inner call to datediff(), you get the current month number since timestamp 0.</p> <pre><code>select datediff(m, 0, getdate() ); 1327 </code></pre> <p>The next part adds that number of months plus 1 to the 0 timestamp, giving you the starting point of the next calendar month.</p> <pre><code>select dateadd( mm, datediff( m, 0, getdate() ) + 1, 0 ); 2010-09-01 00:00:00.000 </code></pre> <p>Finally, the outer dateadd() just subtracts one second from the beginning timestamp of next month, giving you the last second of the current month.</p> <pre><code>select dateadd( s, -1, dateadd( mm, datediff( m, 0, getdate() ) + 1, 0 ) ); 2010-08-31 23:59:59.000 </code></pre> <p><hr/> <strong>This old answer (below) has a bug where it doesn't work on the last day of a month that has more days than the next month. I'm leaving it here as a warning to others.</strong></p> <p>Add one month to the current date, and then subtract the value returned by the DAY function applied to the current date using the functions DAY and DATEADD.</p> <pre><code>dateadd(day, -day(getdate()), dateadd(month, 1, getdate())) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload