Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
2012-06-22 00:00:00.000 577169.990000
2012-06-30 00:00:00.000 794.990000
2012-06-30 00:00:00.000 256154.990000
2012-06-30 00:00:00.000 295904.990000------ i want this line.
2012-07-05 00:00:00.000 1096469.980000
2012-07-10 00:00:00.000 2384.980000
2012-07-28 00:00:00.000 161384.970000
2012-07-28 00:00:00.000 368084.960000
2012-07-28 00:00:00.000 1085729.960000
2012-07-28 00:00:00.000 1228829.960000------ i want this line.
2012-08-02 00:00:00.000 1341719.970000
2012-08-02 00:00:00.000 541719.970000
2012-08-07 00:00:00.000 1341719.970000
2012-08-07 00:00:00.000 961384.970000
2012-08-27 00:00:00.000 161384.970000------ i want this line.
2012-09-10 00:00:00.000 2384.970000
2012-09-25 00:00:00.000 619290.570000
2012-09-27 00:00:00.000 3790.570000
2012-09-29 00:00:00.000 84790.560000
2012-09-29 00:00:00.000 441190.550000------ and want this line.


how can it be done..

i have tried
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, '2012-06-01') + 1, 0))
but it select only all the last dates like
CSS
2012-06-30 00:00:00.000 794.990000
2012-06-30 00:00:00.000 256154.990000
2012-06-30 00:00:00.000 295904.990000
Posted
Comments
Peter Leow 11-Dec-13 3:35am    
You want the last record of every month, right? But you need time element to determine that especially if there are multiple records on the last date of a month.
xibit89 11-Dec-13 3:36am    
yep i need the last record of every month. So, how could i determine it?

You could use this query.

Select datetime,value from Table group by year(datetime),month(datetime) having datetime=max(datettime)
 
Share this answer
 
Hello ,

try this

SQL
--first declare a given date
declare @date date

--declare  day difference of first and last day of a month as int
declare @daydifference int


--declare a lastday as date

declare @lastday date


--set a given date in the variable

set @date='2016-2-12'


--get the daydifference  of first and last day

set @daydifference=(select datediff(day, dateadd(day, 1-day(@date), @date),
              dateadd(month, 1, dateadd(day, 1-day(@date), @date))))


--get the firsdtday of a month

DECLARE @firstDayOfMonth date = CAST( CAST(YEAR(@date) AS varchar(4)) + '-' + 
CAST(MONTH(@date) AS varchar(2)) + '-01' AS date)    
                                  

--now get the lastday of a month

set @lastday=(select DATEADD(DAY, @daydifference-1,@firstDayOfMonth))
 select @lastday


it will show '2016-2-29' as output
thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900