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 ALLSELECT 'New Jersey', '08401' UNION ALLSELECT 'California', '93765' UNION ALLSELECT 'California','94706' UNION ALLSELECT 'New York','11235' GOCREATE FUNCTION dbo.udf_GetZipByState(@statename varchar(28))RETURNS VARCHAR(1000) ASBEGIN DECLARE @ziplist varchar(1000) SELECT @zipList = COALESCE(@zipList + ', ', '') + s.zip FROM Statezip s WHERE s.state = @statename RETURN @ziplistENDGOSELECT distinct state as StateName, dbo.udf_GetZipByState(state) as 'Zip Codes'FROM statezip s
Future guru in the making.