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 |
|
RichDef
Starting Member
2 Posts |
Posted - 2003-03-13 : 12:22:53
|
| Hello There!I have a single table of News stories each with a DateAdded. I want to draw out the news stories based on the Date for example I want the data to be organized by the year.Example output:2000Story1 1/1/2000story2 2/1/2000....2001Story56 1/1/2001story57 2/1/2001...2002How would I construct a query to create a result set like this?Thanks.-Rich |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-03-13 : 12:24:55
|
| Have a look at the DATEPART function in SQL Server Books Online. For example:SELECT DATEPART(year, GETDATE())You will get 2003 back as the year for the current date if your SQL Server has the correct date configured.Tara |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2003-03-13 : 12:26:44
|
| Well, you are going to want to use a SELECT. You can use predicate logic in your WHERE clause to chose the rows you want. You could, potentially, have a SELECT for each year and then UNION those together.Jay White{0} |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-03-13 : 14:06:11
|
| Please use a report writer or ASP page .Select Year(Date), *FromYourTableis all SQL needs to do. The rest should be done by the client.- Jeff |
 |
|
|
RichDef
Starting Member
2 Posts |
Posted - 2003-03-13 : 16:14:50
|
| I was hoping to avoid using individual select statements for each year! But, I may have no choice. You see I wanted to create an all in one solution that would create the yearly break downs etc from the data in the table - rather than doing it on the client side. Using the Union suggestion I would have to get the range of dates - particularly the year ranges (I won't know how far back the stories go) and then generate the Select statements based on each year and then put them together. Its essentially the same thing you had suggested.Either way it looks like I will have to do it the old fashioned way!!Thank you all for your suggestions!-Rich |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-13 : 16:31:17
|
| OK,How about:CREATE TABLE Table1 (Col1 datetime, col2 varchar(25))GoINSERT INTO Table1 (Col1, Col2)SELECT '01/01/2000','Article 1'Union AllSELECT '02/01/2000','Article 2'Union AllSELECT '03/01/2000','Article 3'Union AllSELECT '01/01/2001','Article 4'Union AllSELECT '02/01/2001','Article 5'Union AllSELECT '03/01/2001','Article 6'Union AllSELECT '01/01/2002','Article 7'GoSELECT IsNull(Convert(varchar(25),Col1), '') , IsNull(Col2, '') , CASE WHEN Col2 Is Null THEN ArticleYear Else Null End FROM (SELECT DISTINCT null As Col1, null As Col2, YEAR(Col1) As ArticleYear, 1 As RowGroupingFROM TABLE1Union AllSELECT Col1, Col2, YEAR(Col1) As ArticleYear, 2 As RowGroupingFROM TABLE1) As XXXORDER By ArticleYear, RowGrouping, Col1Drop Table Table1Brett8-) |
 |
|
|
|
|
|
|
|