break column | SQL Server Performance Forums

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=’:)‘ />]<br />Using Tahsins’ DDL (and also assuming that there are no spaces in the city name).<br /><pre id="code"><font face="courier" size="2" id="code"><br />SELECT <br /> PARSENAME(REPLACE(REPLACE(CSZ,’,’,”), ‘ ‘, ‘.’),3) AS City<br /> , PARSENAME(REPLACE(REPLACE(CSZ,’,’,”), ‘ ‘, ‘.’),2) AS State<br /> , PARSENAME(REPLACE(REPLACE(CSZ,’,’,”), ‘ ‘, ‘.’),1) AS ZIP<br /> FROM MyTable<br /></font id="code"></pre id="code"><br />I hope, you need this in order to properly normalise your table structure.<br /><br />–<br />Frank Kalis<br />Microsoft SQL Server MVP<br /<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br />Heute schon gebloggt?<a target="_blank" href=http://www.insidesql.de/blogs>http://www.insidesql.de/blogs</a><br />Ich unterstuetze PASS Deutschland e.V. <a target="_blank" href=http://www.sqlpass.de>http://www.sqlpass.de</a>)
&gt;&gt;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=’:)‘ />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
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=’:)‘ />]<br /><br />Anyway, here’s a further way…<br /><br /><font color="blue"><pre id="code"><font face="courier" size="2" id="code"><font color="green">–data</font id="green"><br />declare @t table (csz varchar(60))<br />insert @t (csz) values (‘Some City GA, 48053’)<br /> <br /><font color="green">–calculation</font id="green"><br />select<br /> left(csz, charindex(‘,’, csz) – 4) as city,<br /> substring(csz, charindex(‘,’, csz) – 2, 2) as state,<br /> substring(csz, charindex(‘,’, csz) + 2, 60) as zip<br />from @t</font id="code"></pre id="code"></font id="blue">Assuming that there are no <i>commas</i> in the city name (if there are, the reverse function will probably have to come into play) [<img src=’/community/emoticons/emotion-2.gif’ alt=’:D‘ />]<br /><br />Oh, and here’s a very similar request (so you know you’re not the only one)…<br /<a target="_blank" href=http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=63847>http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=63847</a><br /><br /><br />Ryan Randall<br />www.monsoonmalabar.com London-based IT consultancy <br /><br />Solutions are easy. Understanding the problem, now, that’s the hard part.
]]>