Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 how do I parse country and state to seperate 2 col
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Monhana
Starting Member

5 Posts

Posted - 08/12/2013 :  17:43:26  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3873 Posts

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

bandi
Flowing Fount of Yak Knowledge

India
2241 Posts

Posted - 08/13/2013 :  00:40:42  Show Profile  Reply with Quote
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 - 08/20/2013 :  14:07:16  Show Profile  Reply with Quote
Thank you very much for your time to reply.

It works and I got output that expected.

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2241 Posts

Posted - 08/21/2013 :  00:55:57  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000