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


Using Substring and Charindex to split a text in to columns


Using Substring and Charindex to split a text in to columns

Author
Message
eidjazparwaz
eidjazparwaz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 104
Hello everyone,

I am struggeling with below:
I have a field in ReportServer DB called “C.Path AS OrgInput (VARCHAR)”. In this field the data has been saved like:

-/Adhoc/CRM/Complaints report/Report on owner level
-/Adhoc/Finance/General Ledger/Spend Analysis report

I am trying to achieve by using substring and charindex to split the data of this field in to columns separated by slash "/". So the result of "OrgInput" would be something like this:

Column1 | Column2 | Column3 | Column4
--------------------------------------------------------------------
/Adhoc | /CRM | /Complaints report | /Report on owner level
/Adhoc | /Finance | /General Ledger | /Spend Analysis report

Is there anyone who can help me or give me some advice of where to start.
This is my query so far:



SELECT C.Path AS OrgInput
, CHARINDEX('/', C.Path, 2) AS [FirstPos]
, SUBSTRING(C.Path -- String
, CHARINDEX('/', C.Path, 2)+1 -- StartPos
, LEN(C.Path) -- Length
) AS StartText

, LEFT(SUBSTRING(C.Path -- String
, CHARINDEX('/', C.Path, 2)+1 -- StartPos
, LEN(C.Path) -- Length
)
, 8
) AS FirstResult

, LEFT(SUBSTRING(C.Path -- String
, CHARINDEX('/', C.Path, 2)+1 -- StartPos
, LEN(C.Path) -- Length
)
, CHARINDEX('/', C.Path, (CHARINDEX('/', C.Path, 2)+1)) -- Test toegevoegd
) AS SecondResult



, CHARINDEX('/', C.Path, (CHARINDEX('/', C.Path, 2)+1)) AS [Second/]
, SUBSTRING(C.Path
, CHARINDEX('/', C.Path, (CHARINDEX('/', C.Path, 2)+1))+1
, LEN(C.Path)
)
FROM dbo.Catalog C WITH(NOLOCK) -- 773 Report Catalog
WHERE 1=1
AND C.Type = 2
ORDER BY 1

/*
SUBSTRING(Text, Start, Length)
LEFT(Text, Length) --> Keep 3 most left chars
RIGHT(Text, Length) --> Keep 3 most right chars
LEN(Text) --> Length
*/




BR,
Eidjaz
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
Hi,

I would start by reading this article first, http://www.sqlservercentral.com/articles/Tally+Table/72993/

Its got a great String splitter function.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
eidjazparwaz
eidjazparwaz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 104
Jason-299789 (12/18/2012)
Hi,

I would start by reading this article first, http://www.sqlservercentral.com/articles/Tally+Table/72993/

Its got a great String splitter function.


Thanks i will read the article and see if i can get wiser Wink.

Eidjaz
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
Here is an example using the DelimitedSplit8K function that you can find at the link provided, or you can follow the link in my signature for splitting strings.


;with cte (Col1) as
(
select '/Adhoc/CRM/Complaints report/Report on owner level' union all
select '/Adhoc/Finance/General Ledger/Spend Analysis report'
)

select *
from cte
cross apply dbo.DelimitedSplit8K(right(Col1, len(Col1) -1), '/')



Notice how I posted data in an easily consumable format. You should consider that on future posts.

Also, I noticed you are using the NOLOCK hint. Are you aware of the challenges that hint brings to the table? It will help your code run faster but it comes at a cost. Most people say "I don't care about dirty reads". This hint has far greater implications than just dirty reads. You can missing or even duplicate information. Here are a couple of articles that explains this hint in greater detail.

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx

_______________________________________________________________

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
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
eidjazparwaz
eidjazparwaz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 104
Sean Lange (12/18/2012)
Here is an example using the DelimitedSplit8K function that you can find at the link provided, or you can follow the link in my signature for splitting strings.


;with cte (Col1) as
(
select '/Adhoc/CRM/Complaints report/Report on owner level' union all
select '/Adhoc/Finance/General Ledger/Spend Analysis report'
)

select *
from cte
cross apply dbo.DelimitedSplit8K(right(Col1, len(Col1) -1), '/')



Notice how I posted data in an easily consumable format. You should consider that on future posts.

Also, I noticed you are using the NOLOCK hint. Are you aware of the challenges that hint brings to the table? It will help your code run faster but it comes at a cost. Most people say "I don't care about dirty reads". This hint has far greater implications than just dirty reads. You can missing or even duplicate information. Here are a couple of articles that explains this hint in greater detail.

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx


Thanks a alot for the example. It really did help me, this is how my qeury look like:
SELECT(   SELECT C.Path AS Path, C.Name AS ReportName
, CASE WHEN C.Hidden = 1 THEN 'True'
ELSE 'False' END AS ReportHide
, COALESCE(C.Description, '<none>') AS ReportDesc
, UC.UserName AS ReportCreatedBy, C.CreationDate AS ObjectCreatedDate
, UM.UserName AS ReportModifiedBy, C.ModifiedDate AS ObjectModifedDate
, D.*
FROM dbo.Catalog C
INNER JOIN dbo.Users UC WITH(NOLOCK)
ON C.CreatedByID = UC.UserID
INNER JOIN dbo.Users UM WITH(NOLOCK)
ON C.ModifiedByID = UM.UserID
CROSS APPLY Ceyenne_JDE.dbo.DelimitedSplit8K(C.Path, '/') D
WHERE 1=1
AND C.Type = 2
-- AND C.Path = '/Adhoc/CRM/20121024 CRM complaints report on owner level'
) Q
PIVOT ( MAX(Item)
FOR ItemNumber IN ([2], [3], [4], [5], [6], [7], [8])
) AS pvt
)


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