November 18, 2014 at 2:47 am
Hi Sir,
I need your help to resolve this issue
I dont know the exact position of '/',',','--' or any special character.
I have to fetch first name of the country.
create table abc
(country nvarchar(200))
go
INSERT INTO abc VALUES ('Tennessee/Virginia,USA')
GO
INSERT INTO abc VALUES ('Kansas,USA')
GO
INSERT INTO abc VALUES ('Ohio, USA')
GO
INSERT INTO abc VALUES ('JAPAN--(Kagoshima and Miyazaki)')
GO
INSERT INTO abc VALUES ('SouthDakota/Iowa,USA')
GO
INSERT INTO abc VALUES ('INDIA--(parts of Gujarat')
GO
INSERT INTO abc VALUES ('USA--(New Jersey)')
GO
Below I want the result from my table abc
Tennessee
Kansas
Ohio
JAPAN
SouthDakota
INDIA
USA
November 18, 2014 at 3:05 am
You could replace all of the special characters with one common special character, for example |.
Then you could split the data using a splitter function[/url] and get the first term.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
November 18, 2014 at 3:06 am
ps: Tennessee, Kansas, Ohio and SouthDakota are not countries, but states.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
November 18, 2014 at 3:10 am
by making the assumptions outlined below, you can use patindex to find the beginning of the first non-character letter, and grab the substring.
the desired string always starts the string.
some character that is NOT A-Z is the delimiter.
note you need to take the position into consideration, which uis why my second column removes one from where the pattern starts, which leaves you with just the desired string.
select substring(country,1,patindex('%[^A-Z]%',country) ) ,
substring(country,1,patindex('%[^A-Z]%',country) -1 ) from abc
/*results
----- --------------------------------------
Tennessee/ Tennessee
Kansas, Kansas
Ohio, Ohio
JAPAN- JAPAN
SouthDakota/ SouthDakota
INDIA- INDIA
*/
Lowell
November 18, 2014 at 3:59 am
Thanks Sir for your solution it worked.
but getting error while executing my code by adding your solution for getting country name.
"Invalid length parameter passed to the LEFT or SUBSTRING function"
I have some below data as well in my table
INSERT INTO abc VALUES ('(Part of) AUSTRIA')
GO
INSERT INTO abc VALUES ('(Part of) AUSTRIA; CROATIA; HUNGARY; SLOVENIA')
GO
INSERT INTO abc VALUES ('(Part of) SWEDEN')
GO
from this i would like fetch
AUSTRIA
AUSTRIA
SWEDEN
Regards,
Kiran
November 18, 2014 at 4:03 am
It would be nice if you would state all of your requirements in your initial question, instead of changing them halfway.
The simplest solution would be to replace '(Part of) ' with an empty string.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
November 18, 2014 at 4:13 am
yep your error goes directly to y assumptions: based on the sample data, the desired string always exists at the beginning.
Koen's suggestion is easily the fastest, but there's probably other data that also need to be tweaked.
the desired string always starts the string.
some character that is NOT A-Z is the delimiter.
Lowell
November 18, 2014 at 6:29 am
Thanks Sir for your reply its working for me thanks a lot
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply