Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL parsing in a proc

Posted on 2012-04-06
11
Medium Priority
?
314 Views
Last Modified: 2012-06-21
How can I parse a string in a proc?
0
Comment
Question by:HLRosenberger
  • 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 2000 total points
ID: 37816282
just check @cnt before inserting:


                  IF @Cnt > 1
                        Insert Into @RtnValue (data)
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

927 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