Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Last month

Author  Topic 

medmidou
Starting Member

4 Posts

Posted - 2009-08-28 : 09:30:36
Hello

I want to make w where clause according to the last month,
somthing which will lokk like this :

Select date, offered, answered
from my_table
where date = ""last_month""

Thankyou for your help

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-28 : 09:36:51
So, you want to select only last month's data?

Select date, offered, answered
from my_table
where date>=dateadd(month,datediff(month,0,getdate())-1,0) and
date<dateadd(month,datediff(month,0,getdate()),0)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

medmidou
Starting Member

4 Posts

Posted - 2009-08-28 : 10:22:35
thanks it works too, :))

i even test it to get the last week data,
anyway, how can i extract the last week number or the last month number,
Go to Top of Page

ScottWhigham
Starting Member

49 Posts

Posted - 2009-09-03 : 06:14:26
Just add new columns into the SELECT to use either DATEPART, DATENAME, or MONTH functions (look them up in Books Online):

SELECT date, offered, answered, DATEPART(month, date) AS MonthNumber1, MONTH(date) AS MonthNumber2, DATEPART(week, date) AS WeekNumber

========================================================

I have about 1,000 video tutorials on SQL Server 2008, 2005, and 2000 over at http://www.learnitfirst.com/Database-Professionals.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-03 : 06:20:40
quote:
Originally posted by ScottWhigham

Just add new columns into the SELECT to use either DATEPART, DATENAME, or MONTH functions (look them up in Books Online):

SELECT date, offered, answered, DATEPART(month, date) AS MonthNumber1, MONTH(date) AS MonthNumber2, DATEPART(week, date) AS WeekNumber

========================================================

I have about 1,000 video tutorials on SQL Server 2008, 2005, and 2000 over at http://www.learnitfirst.com/Database-Professionals.aspx


Note that OP wanted to get week number or month number of last month and not based on a column

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -