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.
| Author |
Topic |
|
medmidou
Starting Member
4 Posts |
Posted - 2009-08-28 : 09:30:36
|
| HelloI want to make w where clause according to the last month,somthing which will lokk like this :Select date, offered, answeredfrom my_tablewhere 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, answeredfrom my_tablewhere date>=dateadd(month,datediff(month,0,getdate())-1,0) anddate<dateadd(month,datediff(month,0,getdate()),0)MadhivananFailing to plan is Planning to fail |
 |
|
|
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, |
 |
|
|
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 |
 |
|
|
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 columnMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|