Returning Max date in SQL Server -


i'm trying return max dates of input dates.

create procedure [dbo].[getmaxmonthdate]     @from datetime,     @to datetime begin     set nocount on;      select max(day(ds.datum)) daymax,            month(ds.datum) monthmax,            year(ds.datum) yearmax,            cast(convert(varchar, year(ds.datum)) + '-' + convert(varchar, month(ds.datum)) + '-' + convert(varchar, max(day(ds.datum))) datetime) datemax      qr_ds022s ds     ds.datum >= @from , ds.datum <= @to     group month(ds.datum), year(ds.datum), ds.datum end 

for example when i'm typing

@from='2014-01-01' , @to='2014-04-01' result of query follows:

daymax | monthmax | yearmax | datemax ========================================   31   |    1     |   2014  | 2014-01-31   28   |    2     |   2014  | 2014-02-28   31   |    3     |   2014  | 2014-03-31   01   |    4     |   2014  | 2014-04-01 

currently i'm getting dates want last date of month. how can resolve this?

add clause where, depending on sql version logic changes

for sql2012-onwards use eomonth

... ds.datum >= @from , ds.datum <= @to , eomonth(ds.datum) = ds.datum ... 

pre-sql2012 add 1 day , check same month

... ds.datum >= @from , ds.datum <= @to , month(dateadd(day,1,ds.datum)) != month(ds.datum) ... 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -