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 2000 Forums
 SQL Server Development (2000)
 SELECT help

Author  Topic 

Access
Starting Member

44 Posts

Posted - 2007-11-30 : 11:20:42
I have a table:



In output i need to have something like this


Thank you

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-11-30 : 12:04:47
This will do:


CREATE TABLE STATEZIP (
STATE varchar(28),
ZIP char(5)
)
INSERT STATEZIP (STATE, ZIP)
SELECT 'New York', '11229'
UNION ALL
SELECT 'New York', '11230'
UNION ALL
SELECT 'New Jersey', '08401'
UNION ALL
SELECT 'California', '93765'
UNION ALL
SELECT 'California','94706'
UNION ALL
SELECT 'New York','11235'

GO

CREATE FUNCTION dbo.udf_GetZipByState(@statename varchar(28))
RETURNS VARCHAR(1000) AS

BEGIN
DECLARE @ziplist varchar(1000)

SELECT @zipList = COALESCE(@zipList + ', ', '') + s.zip
FROM Statezip s
WHERE s.state = @statename

RETURN @ziplist
END

GO

SELECT distinct state as StateName, dbo.udf_GetZipByState(state) as 'Zip Codes'
FROM statezip s





Future guru in the making.
Go to Top of Page

Access
Starting Member

44 Posts

Posted - 2007-11-30 : 12:11:24
That did it,
Thank you
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-01 : 02:33:53
Also read http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx

Madhivanan

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

- Advertisement -