SQL Server Forums
Profile | Register | 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
 New Topic  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

3589 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
2206 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
2206 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  
 New 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.05 seconds. Powered By: Snitz Forums 2000