Solved

SQL parsing in a proc

Posted on 2012-04-06
11
305 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 69

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 69

Accepted Solution

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


                  IF @Cnt > 1
                        Insert Into @RtnValue (data)
0
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 69

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to check the index used by a MS SQL queries or SP 7 37
T SQL Update Table from another table 5 42
SQL Query 34 79
SQL server is using more virtual memory. 5 63
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now