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

Sql Query that will return last one month data, last two months data and last one week in sql server 2005

Posted on 2009-05-12
7
1,050 Views
Last Modified: 2012-05-06
Hi,

I need a sql query that will return all data for last one month, last two months ans last one week.

I have one smalldatetime column. Alos, I m using SQl server 2005.

Thanks

0
Comment
Question by:Saroj13
7 Comments
 
LVL 6

Expert Comment

by:bull_rider
ID: 24367441
Do you want all of them in one single sql statement? I am surprised.
0
 
LVL 1

Accepted Solution

by:
Samoin earned 200 total points
ID: 24367457
Can you be little bit more specific? Any ways below is the query that might help you

SELECT * FROM [YourTableName]
WHERE [date_column] Between GETDATE() And DATEADD(MM,-1,GETDATE())  --To get data for last one month
And [date_column] between GETDATE() and DATEADD(MM,-2,GETDATE()) -- To get data for last two months
And [date_column] between GETDATE() and DATEADD(ww,-1,GETDATE()  -- To get data for last week
0
 
LVL 6

Assisted Solution

by:bokist
bokist earned 150 total points
ID: 24367539
Check this solution :

select * from my_table
where datepart(year, my_date) = datepart(year, getdate())
   and  datepart(month, my_date) = datepart(month, getdate()) // or getdate())-1,,, -2
   and  datepart(week, my_date) = datepart(week, getdate())

0
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.

 

Author Comment

by:Saroj13
ID: 24368305
little change.

I want data for last one month.
Today is May12, I want data from may12 -april12, 2009.

I need the sql query for that.

thanks
0
 
LVL 6

Assisted Solution

by:bokist
bokist earned 150 total points
ID: 24369195
select * from my_table
where datepart(year, my_date) = datepart(year, getdate())
   and  datepart(month, my_date)  := datepart(month, getdate())  - 1

0
 
LVL 6

Expert Comment

by:bokist
ID: 24369229
in third line didn't appear greater sign.
something is wrong with my keyboard.
third line is greater or equal
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 150 total points
ID: 24370260

select *
  from my_table
 where my_date between DATEADD(mm,-1,GETDATE()) and GETDATE()

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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