[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extracting String

Posted on 2005-05-01
10
Medium Priority
?
248 Views
Last Modified: 2010-03-19
Hi,

How do I extract a word and sum up my results as per that word from a field? For example,
I've a table with columns Branch, Name, Count

Branch                              Name                Count
Universe_Branch1_Atl         Atlanta             23
Universe1_Branch1_Cn        Cincinatti         10
Universe2_Branch2_Ch        Chicago           15

I want to output results by grouping by Branch1 and Branch2 (rather than displaying the whole word, I just want to display Branch1 or Branch2)as follows:

Branch1    Atlanta      23
Branch1    Cincinatti   10
Branch2    Chicago     15


Thanks
0
Comment
Question by:SKM061104
8 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 13905721
Something like this should be on the right track.  Basically, you can use Charindex to see if the string exists in your column and then group by that:

select count(*),
case when charindex('1',branch)>0 THEN 'Branch1'
        when charindex('2',branch)>0 then 'Branch2'
end as BranchName
from yourtable
group by
case when charindex('1',branch)>0 THEN 'Branch1'
        when charindex('2',branch)>0 then 'Branch2'
end
0
 
LVL 9

Expert Comment

by:paelo
ID: 13905731
If the format is always like that, you can use:

SELECT SUBSTRING(y.yourfld, CHARINDEX('_',y.yourfld,0)+1, CHARINDEX('_',y.yourfld,CHARINDEX('_',y.yourfld,0)+1) - CHARINDEX('_',y.yourfld,0)-1) As [Branch]
FROM dbo.yourtable y
GROUP BY SUBSTRING(y.yourfld, CHARINDEX('_',y.yourfld,0)+1, CHARINDEX('_',y.yourfld,CHARINDEX('_',y.yourfld,0)+1) - CHARINDEX('_',y.yourfld,0)-1)

There may be a less CHARINDEX intensive solution.

-Paul.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 13905733
Select SUBSTRING(Branch, CHARINDEX('_', Branch) + 1, CHARINDEX('_', Branch, CHARINDEX('_', Branch) + 1) - 1),
          Name,
          Count
From Table1
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 13905752
Here is a simpler solution (that does not use CHARINDEX):

Select PARSENAME(REPLACE(Branch, '_', '.'), 2)),
          Name,
          Count
From Table1
0
 
LVL 9

Expert Comment

by:paelo
ID: 13905758
Very nice.

-Paul.
0
 

Author Comment

by:SKM061104
ID: 13928613
Hi all,

Sorry, a slight twist to my previous question:
I've  a table, say 'Table1' like below:
Branch                              Name                Count
Universe_Branch1_Atl         Atlanta             23
Universe1_Branch1_Cn        Cincinatti         10
Universe1_Branch1_Cn2      Cincinatti          24
Universe2_Branch2_Ch        Chicago           15


And I want the above to output as follows:

Branch1   Atlanta      23
Branch1   Cincinatti   34
Branch2   Chicago     15

So, what is the easiest way? What would I be grouping by?

Thanks!!
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 13929466
Try it this way:

Select PARSENAME(REPLACE(Branch, '_', '.'), 2)),
          Name,
          Sum(Count) Count
From Table1
Group By PARSENAME(REPLACE(Branch, '_', '.'), 2)), Name
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 14102044
>>Any objections<<
None :)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question