Solved

SQL string iteration function

Posted on 2013-05-20
3
673 Views
Last Modified: 2013-05-20
Morning,
I have the following values in a table which I need to extract the numerical value combinations. All the values follow the same pattern with underscores.
A-FP_1234_5678_EF
Can I pls have some help to create a simple function that will loop thru and return the 2 sets of numbers from the passed string being 1234 and 5678

Thanks
0
Comment
Question by:CraigLazar
[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
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 300 total points
ID: 39180387
give it a whirl

create function dbo.slice
(
 @t varchar(1000)
)
returns @slicedRows Table
(
  NumbericalPart    BIGINT
)
AS
BEGIN
declare @first_ int 
declare @second_ int 
declare @third_ int 

set @first_ = charindex('_',@t)
set @second_ = charindex('_',@t,@first_ + 1 )
set @third_ = charindex('_',@t,@second_ + 1 )
insert into  @slicedRows 
select CAST(substring(@t,@first_+1,@second_- @first_-1) as BIGINT)
UNION ALL
select CAST(substring(@t,@second_+1,@third_-@second_-1) as BIGINT)
return
END

Open in new window


the values are returned when you do the below

select * from dbo.slice('A-FP_1234_5678_EF')

Open in new window


Remember the function does return a table so, you need to do a cross apply if want to join it in a where clause.
0
 
LVL 8

Expert Comment

by:jpgobert
ID: 39180390
There may be a better way but here's what I'm thinking...  If your data always follows that format then you have three underscore characters that you need to know the positions of.

Declare @POS1 int  //position of the first _
Declare @POS2 int  //position of the second _
Declare @POS3 int  //position of the third _
Declare @NumberSet1 varchar(10)  //First string of #'s
Declare @NumberSet2 varchar(10)  //Second string of #'s

// You can use something other than varchar in the last two... i don't know your data...

// The next three lines will set the positions of the _ chars into the three variables

Set @POS1 = CharIndex("_", string, 1)
Set @POS2 = CharIndex("_", string, @POS1+1)
Set @POS3 = CharIndex("_", string, @POS2+1)

// Now that you know where the _ characters are you can use substring to capture them

Set @NumberSet1 = SubString(string, (@POS1+1), (@POS2 - @POS1 - 1))
Set @NumberSet2 = SubString(string, (@POS2 + 1), (@POS3 - @POS2 - 1))


Hope it helps...
0
 
LVL 4

Author Closing Comment

by:CraigLazar
ID: 39180558
Thanks Neo, perfect, just made some adjustements, returning BIGINT instead of table and created 2 functions each for number 1 and number 2
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

728 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