SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to retrieve two sets of values from one column


How to retrieve two sets of values from one column

Author
Message
josetur12
josetur12
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 392
I need help to accomplish the task of getting two values from the string in one column.
I need to extract the first set of characters as regardless of the length until the first forward slash (/) is found.
The second task I need to do is to get the value after the first forward slash until the second back slash is found. (Row IDs 1 and 2 meet this criteria)
For row number 3, there is a forward slash but, it is not in the whole value is not in the same format as the first two rows. In this case I need to ignore this row.

This is the values I need to see based on the sample date

Value1 Value2
357-1329 user one (row 1)

224-57 User Two (row 2)


This is the DDL

Drop table MyTestTable

CREATE TABLE [dbo].[MyTestTable](
[ID] int Not NULL,
[Col1] [varchar](100) NULL)

Insert into MyTestTable
(ID, Col1)
Select 1, '357-1329 / user one / User One/357-1329' UNION ALL
Select 2, '224-57 / User Two / O0427A-85311 EG 8/24' UNION ALL
Select 3, '214-962 User Three PLUMBING SUPPLIES EG 8/30'

Thanks in advance
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7821 Visits: 7145

-- one more test data row
INSERT INTO MyTestTable (ID, Col1) SELECT 4, 'a longer string value1 / a much much much much longer string value2 / whatever / whatever / whatever'

SELECT
Col1,
LEFT(Col1, position_of_first_slash - 1) AS Value1,
SUBSTRING(Col1, position_of_first_slash + 1, CHARINDEX('/', Col1, position_of_first_slash + 1) - position_of_first_slash - 1) AS Value2
FROM dbo.MyTestTable
CROSS APPLY (
SELECT CHARINDEX('/', Col1) AS position_of_first_slash
) AS ca1
WHERE
Col1 LIKE '%/%/%'




SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
josetur12
josetur12
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 392
Thank you, this 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