Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

get max date record entered in previous month

Posted on 2006-11-16
5
307 Views
Last Modified: 2008-03-17
Hello
I have a table where I extract data for a given day - I want to compare this data with data from previous month - but only the data for the last entry for the previous month - how would I do this?

0
Comment
Question by:modish
  • 2
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 17955211
select * from yourtable
where yourfield = ( select max ( yourfield ) from yourtable where yourfield <= dateadd(day, 1 - datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120) )   )
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 200 total points
ID: 17955218
select top 1 *
from urTable  WHERE urDateColumn < CAST(CONVERT(Varchar(6), GETDATE(),112)+'01' as Datetime)
ORDER BY urDateColumn DESC
0
 

Author Comment

by:modish
ID: 17964841
Apologies for the late response
thanks to you both for your responses - I couldn't get aneeshattingals one to work  problem with casting.- but angelIII worked brilliantly -
I changed it to be
 yourfield <= dateadd(day,  - datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120) )  
to get the last day of the previous month ..
Is this the best way to do the same for the month before that.
yourfield <=  dateadd(month,-1,dateadd(day, 1 - datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120) )   )

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17964863
yes. the dateadd(month, -1) ... will take care of any 31/30/29/28 days per month.
0
 

Author Comment

by:modish
ID: 17964885
Excellent and aneeshattingal solution worked as well :-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question