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 2012 Forums
 Transact-SQL (2012)
 how do I parse country and state to seperate 2 col

Author  Topic 

Monhana
Starting Member

5 Posts

Posted - 2013-08-12 : 17:43:26
Hi,

I have fields Country and State that has value:

Rockingham, NH

Vermilion, LA


Can you please help me out with stripe Country Vermilion (without comma) and state is LA


Country: Rockingham
State: NH

Thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-12 : 18:14:59
[code]
SELECT
LEFT(YourColumn, CHARINDEX(',',YourColumn+',')-1) AS County,
STUFF(YourColumn,1,CHARINDEX(',',YourColumn+','),'') AS STATE
FROM YourTable;[/code]
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-08-13 : 00:40:42
if you have only 2 characters in the STATE Code column, you can query as follws
SELECT LEFT( ColumnName, CHARINDEX(',', ColumnName+',')-1) AS Country, 
RIGHT( ColumnName, 2)
FROM TableName;


--
Chandu
Go to Top of Page

Monhana
Starting Member

5 Posts

Posted - 2013-08-20 : 14:07:16
Thank you very much for your time to reply.

It works and I got output that expected.

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-08-21 : 00:55:57
quote:
Originally posted by Monhana

Thank you very much for your time to reply.
It works and I got output that expected.


welcome

--
Chandu
Go to Top of Page
   

- Advertisement -