SQL Server Performance Forum – Threads Archive
break column
Hi I have colum named CSZ for city state and zipcode i want to select from this column three diferent column one for city then state and then zip the data in the file is listed like this Atlant GA, 48053 How can i do this Thanks Thanks!!Is this in the design stage, or is the system already in use? If you’re still in design, then have three separate columns for City, State and Zipcode. If already in use, then you’re out of luck. Also, if the data says "Atlant GA" then you also have a problem with data quality.
Try the following:
This assumes that your City has no spaces and the format is as you mentioned it to be CREATE TABLE MyTable (CSZ VARCHAR(100))
go INSERT INTO MyTable VALUES (‘Atlant GA, 48053’)
INSERT INTO MyTable VALUES (‘LosAngeles CA, 90210’)
go CREATE FUNCTION dbo.CityStateZip
(@IDENTIFIER VARCHAR(10), @CSZ varchar(200))
RETURNS varchar(200) AS BEGIN DECLARE @City VARCHAR(85)
DECLARE @State VARCHAR(3)
DECLARE @Zip VARCHAR (10) SET @City = SUBSTRING(@CSZ,1,CHARINDEX(‘ ‘,@CSZ,1)-1)
SET @State = SUBSTRING(@CSZ,CHARINDEX(‘ ‘,@CSZ,1),LEN(@CSZ))
SET @Zip = SUBSTRING(@CSZ,CHARINDEX(‘ ‘,@CSZ,1)+5,LEN(@CSZ)) IF @IDENTIFIER = ‘City’
BEGIN
SET @CSZ = @City
END
IF @IDENTIFIER = ‘State’
BEGIN
SET @CSZ = @State
END
IF @IDENTIFIER = ‘Zip’
BEGIN
SET @CSZ = @Zip
END RETURN @CSZ END
go SELECT dbo.CityStateZip(‘City’, CSZ) AS City, dbo.CityStateZip(‘State’, CSZ) AS State, dbo.CityStateZip(‘Zip’, CSZ) AS Zip From MyTable
go
DROP FUNCTION CityStateZip
go DROP TABLE MyTable
go
– Tahsin
Read about Normalization
http://www.datamodel.org/NormalizationRules.html Also read this to split it into seperate columns
http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=14124 Madhivanan Failing to plan is Planning to fail
You know, sometimes PARSENAME() comes in really handy. [<img src=’/community/emoticons/emotion-1.gif’ alt=’

>>You know, sometimes PARSENAME() comes in really handy<br /><br />Yes I know. But the problem is if CSV has more than four words, then PARSENAME() will generate NULL values and you need to split it further to work on [<img src=’/community/emoticons/emotion-1.gif’ alt=’

Hi all,<br /><br /> <blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">assuming that there are no spaces in the city name<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">That’s a bit of an assumption! [<img src=’/community/emoticons/emotion-1.gif’ alt=’


]]>