Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Splitting a string from a column into three values


Splitting a string from a column into three values

Author
Message
josetur12
josetur12
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 390
Hi, this is my DDL and sample data for my question.
CREATE TABLE MytestTable(
ID [int],
SplitValue [varchar](40) NULL,
)

Insert into MytestTable
(ID, SplitValue)
SELECT 1, 'DM5FCL-18461-PLANT MANAGEMENT' UNION ALL
SELECT 2, 'OMDPHR-92911-Test Value' UNION ALL
SELECT 3, 'OMMHIM-93611-MEDICAL RECORDS' UNION ALL
SELECT 4, 'S406-20330-Test Services' UNION ALL
SELECT 5, 'SSCN-20118-Network Services'

I need help in writing a query to separate the string in the sample rows into three columns separating them when the dash (-) is found.
For example, the value in row number 1, I need to display the following,

Value1, Value2, Value3
DM5FCL 18461 PLANT MANAGEMENT

I will appreciate any help on this.
Thanks
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36162 Visits: 18751
You can use PATINDEX or CHARINDEX to do this. Here's a sample: http://www.sqlservercentral.com/articles/String+Manipulation/94365/

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14965 Visits: 38984
great jo providing the setup data!

Here's another fast way using the PARSENAME function;
PARSENAME is usually used to split object names, like ServerName.Databasename.dbo.TableName,
but can be used for IP address and other strings, and is limited to 4 parts.
it also thinks the strings are right to left(4,3,2,1), where we would see the string as left to right (1,2,3,4)


note that this assumes your data will not have periods in it,
and is all three parts like your example data.

select
PARSENAME(REPLACE(SplitValue,'-','.'),3) AS PartT1,
PARSENAME(REPLACE(SplitValue,'-','.'),2) AS PartT2,
PARSENAME(REPLACE(SplitValue,'-','.'),1) AS PartT3,
MytestTable.*
FROM MytestTable



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

josetur12
josetur12
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 390
Thank you, this last solution works.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search