October 22, 2009 at 10:09 am
I currently have data in this format:
StateCityCodeName
CT01Hartford
CT02New Haven
CT03Stamford
VA01Herndon
VA02Reston
I need them in this format (like Crosstab):
StateCityCode1Name1 CityCode2Name2CityCode3 Name3
CT01Hartford02New Haven03 Stamford
VA01Herndon02Reston
I was thinking to use pivot but as we use SQL server 2000, Iām unable to use it.
I can limit the max CityCode and Name to 20 columns. Like CityCode1 ā¦ CityCode20.
So anybody can help me with it? I was thinking about cursor, but Iām not familiar with it.
SELECT
State,
CityCode,
Name
FROM tblAddress
WHERE State IN ('CT','VA')
ORDER BY 1,2
October 23, 2009 at 7:54 am
Check out the 2 Cross Tab and Pivot articles linked in my signature. They give a better explanation than I could. They do apply to SQL Server 2000.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
October 23, 2009 at 8:43 pm
Agreed... no need for a Cursor... see the two articles that Jack pointed you to... I know the guy that wrote them. š
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2009 at 9:22 am
Thank you. While I was working after reading the article, my user suggested she wants it in CSV format. Which I can manage.
I know I may get these kinds of requests in future, so I will make a note of these articles.
Once again thank you Jack and Jeff.
October 28, 2009 at 11:00 pm
Hmmmm... CSV format... that's NOT a cross-tab. That's concatenation. Here's another article link. This one shows a couple of methods of concatenation and some of the associated pitfalls with performance. I know this guy pretty well, too! š
http://www.sqlservercentral.com/articles/Test+Data/61572/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply