Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Catch Data between symbols in SQL Expand / Collapse
Author
Message
Posted Wednesday, February 06, 2013 8:34 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:26 AM
Points: 156, Visits: 567
Hi geniuses,

I need to catch some data from a column named AllLevels, that contains, in each line:

Analysis»Letters»Numbers»Detail

I want to be able to separate the data, in order to organize it this way:


LevelI LevelII LevelIII LevelIV
Analysis Letters Numbers Detail



Thanks in advance.
Regards

Post #1416551
Posted Wednesday, February 06, 2013 8:55 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606, Visits: 8,247
Can you post ddl and sample data. See the first link in my signature.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1416563
Posted Wednesday, February 06, 2013 9:06 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:26 AM
Points: 156, Visits: 567
I'm creating the query from scratch, can't really expose data.
Can you start based on that? This is mostly a query problem.

AllLevels
Analysis»Letters»Numbers»Detail
Description»Letters»Numbers»Detail
Planning»Letters»Symbols»Detail


Thanks in advance.
Regards
Post #1416575
Posted Wednesday, February 06, 2013 9:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 14, 2013 3:16 PM
Points: 182, Visits: 542
As always the case with matching/parsing tasks, it's helpful to actually see the real data to be able to offer a meaningful solution/advise.

If you cannot expose sensitive data, can you make an effort and generate a couple [or more] of bogus records that are similar to the original ones in complexity and pattern?
Post #1416580
Posted Wednesday, February 06, 2013 9:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:39 PM
Points: 11,638, Visits: 27,713
are there deeper than 4 levels in the "real" data? are there always exactly four levels?

the right solution depends a lot on some assumptions we can make with the data

here's just one example, using PARSENAME, which expects a maximum of four sections:

With MySampleData (AllLevels)
AS
(
SELECT 'Analysis»Letters»Numbers»Detail' UNION ALL
SELECT 'Description»Letters»Numbers»Detail' UNION ALL
SELECT 'Planning»Letters»Symbols»Detail'
)

SELECT
PARSENAME(REPLACE(AllLevels,'»','.'),4) As Level1,
PARSENAME(REPLACE(AllLevels,'»','.'),3) As Level2,
PARSENAME(REPLACE(AllLevels,'»','.'),2) As Level3,
PARSENAME(REPLACE(AllLevels,'»','.'),1) As Level4
FROM MySampleData

other possibilities are using a custom function to do the same, or the DelimitedSplit8K function and a PIVOT to get the data in the desired format.
here, a scalar function seems to be an easier implementation for me, i think.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1416587
Posted Wednesday, February 06, 2013 9:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:31 AM
Points: 3,367, Visits: 1,563
You know I was about to reply with a query to parse the string out but then noticed this in Sean's signature.

Need to split a string? Try Jeff Moden's splitter.

I would read that and try out the function he has near the bottom. It will split your string into multiple rows. Use cross apply to join it to your table, then you can group the different rows back together to give you your columns.

Or you can write some parsing code using LEFT, SUBSTRING and CHARINDEX. Even assuming that all 4 levels will always be there it isn't exactly trivial (at least not for me.)

Here is a start to it. You can see using Jeff's splitter might be easier though.

declare @temp varchar(250)

SET @temp = 'Analysis»Letters»Numbers»Detail'

SELECT LEFT(@temp,charindex('»',@temp)-1),
SUBSTRING(@temp, charindex('»',@temp)+1, charindex('»',@temp,charindex('»',@temp)+1)-charindex('»',@temp)-1)



Kenneth Fisher
I strive to live in a world where a chicken can cross the road without being questioned about its motives.
--------------------------------------------------------------------------------
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Link to my Blog Post --> www.SQLStudies.com
Post #1416591
Posted Wednesday, February 06, 2013 9:28 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 9:50 AM
Points: 2,019, Visits: 4,923
It is very easy to create a small script that creates the table and insert the data into the table. Since it does save some time from anyone that tries to help you, you should include in your question such a script. Here is a small script that creates the table and inserts data:


create table Demo (vc varchar(200))
go

insert into Demo (vc) values ('Analysis»Letters»Numbers»Detail')
insert into Demo (vc) values ('Description»Letters»Numbers»Detail')
insert into Demo (vc) values ('Planning»Letters»Symbols»Detail')


If you don't have dots inside the strings, you can use the function parsename. This function is intended to parse full name of objects that are referenced in the database and give the name of the server or database or schema or object. Since those parts are separated by period, the code bellow will work if there is no period inside your strings. If you can have periods inside the strings, you'll have to modify it.
select parsename(replace(vc,'»','.'),4), parsename(replace(vc,'»','.'),3), parsename(replace(vc,'»','.'),2), parsename(replace(vc,'»','.'),1)
from Demo


Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1416596
Posted Wednesday, February 06, 2013 10:06 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:26 AM
Points: 156, Visits: 567

Thanks Guys.

Lowell, yes there are more levels indeed.

Regards
Post #1416626
Posted Wednesday, February 06, 2013 10:09 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606, Visits: 8,247
davdam8 (2/6/2013)

Thanks Guys.

Lowell, yes there are more levels indeed.

Regards


Then I would suggest you look at the article in my signature about splitting strings. Then look at the articles in my signature about cross tabs and pivots. The techniques found in those will get what you need.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1416629
Posted Thursday, February 07, 2013 2:59 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 20, 2013 12:37 AM
Points: 1,049, Visits: 1,439
I worked it out. This is the query that would get you the results you want:

--Creating Table 

Create Table Ex1
(
Id Int Identity(1,1),
AllLevels Varchar(MAX)
)


--Inserting Sample Data

Insert Into Ex1
Select 'Analysis»Letters»Numbers»Detail'
Union ALL
Select 'Description»Letters»Numbers»Detail'
Union ALL
Select 'Planning»Letters»Symbols»Detail'


--Query for your requirement

Select Id,
MAX(Case When rn = 'Level1' Then df Else '' End) As Level1,
MAX(Case When rn = 'Level2' Then df Else '' End) As Level2,
MAX(Case When rn = 'Level3' Then df Else '' End) As Level3,
MAX(Case When rn = 'Level4' Then df Else '' End) As Level4
From
(
Select Id, AllLevels, a.df, 'Level' + Cast(ROW_NUMBER() Over(Partition By Id Order By Id) As Varchar) As rn From Ex1 As b
Cross Apply
(
SELECT SUBSTRING('»' + b.AllLevels + '»',N+1,CHARINDEX('»','»' + b.AllLevels + '»',N+1)-N-1) As df
FROM dbo.Tally As a
WHERE a.N < LEN('»' + b.AllLevels + '»')
AND SUBSTRING('»' + b.AllLevels + '»',N,1) = '»'
) a
) As p
Group By Id



But, it takes a 8 looooong seconds to get the result for this teeeny sample data. So, for tables with huge amounts of data this would definitely not be a good one.
This means that your design is not good. Inserting Delimited strings and pivoting them is not what SQL Server is meant to do.
You should rather do it with a reporting tool.

This is what I could come up with...may be someone might come up with something better.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1416913
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse