Solved

SQL parsing in a proc

Posted on 2012-04-06
11
311 Views
Last Modified: 2012-06-21
How can I parse a string in a proc?
0
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37816175
what kind of parsing do you want to do?
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37816249
Actually, I found a way to do it, but it needs a tweak.

here's a call to my proc:

select * from dbo.Split_PValues('1111 annual X1212121  annual Xhghghf')  

it returns a table:

1  1111
2  X1212121
3  Xhghghf

However, I do not want the "row" before the first "annual".  As output I want:

1  X1212121
2  Xhghghf


here's the proc:


BEGIN
      Declare @Cnt int
      Set @Cnt = 1
      Declare @delimiter nvarchar(6)
      Set @delimiter = 'Annual'

      While (Charindex(@delimiter,@RowData)>0)
      Begin
            Insert Into @RtnValue (data)
            
            Select Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@delimiter, @RowData)-1)))
            
            Set @RowData = Substring(@RowData,Charindex(@delimiter, @RowData) + len(@delimiter),len(@RowData))
                                                
            Set @Cnt = @Cnt + 1
      End
      
      Insert Into @RtnValue (data)
      Select Data = ltrim(rtrim(@RowData))

      Return
END
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 37816282
just check @cnt before inserting:


                  IF @Cnt > 1
                        Insert Into @RtnValue (data)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 32

Expert Comment

by:awking00
ID: 37816304
select * from dbo.Split_PValues(substring(yourfield,charindex('annual',yourfield),len(yourfield) - charindex('annual',yourfield)))
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 37816311
perfect.  Thanks!
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37816335
ah, one last question:

What I want to split is actually a text column in a table.

So instead of this:

select * from dbo.Split_PValues('1111 annual X1212121  annual Xhghghf')  

instead, effectively I want to do this:

select * from dbo.Split_PValues (select *  from CstFldDt
where CstID = 76 and CompanyID = 15 and Caption1 = 'category')  

But syntactically this does not work.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37816338
I mean this:

select pvalues from dbo.Split_PValues (select *  from CstFldDt
where CstID = 76 and CompanyID = 15 and Caption1 = 'category')
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37816344
sorry.  I mean this:

select * from dbo.Split_PValues (select Pvalues  from CstFldDt
where CstID = 76 and CompanyID = 15 and Caption1 = 'category')
0
 
LVL 32

Expert Comment

by:awking00
ID: 37816374
Perhaps
Set @RowData = select Pvalues  from CstFldDt where CstID = 76 and CompanyID = 15 and Caption1 = 'category'
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37816382
you need a cross apply:


select  *
from CstFldDt AS C
CROSS APPLY dbo.Split_PValues(C.PValues) AS PV
where C.CstID = 76
and C.CompanyID = 15
and C.Caption1 = 'category'
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37816410
emoreau - Thanks again!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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