dbTalk Databases Forums  

I need help on string function

comp.databases.sybase comp.databases.sybase


Discuss I need help on string function in the comp.databases.sybase forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sandeep
 
Posts: n/a

Default I need help on string function - 04-14-2010 , 12:12 PM






I have a requirement in my project

one column is getting values like

sdfjsdlj.kfjdj.abc.bombay.wue
ikekeke.kfjdj.abc.pune.uro
woeek.kfjdj.lmn.chennai.mno
oflfk.kfjdj.jky.hydrabad.xyz
eiodkk.woek.skssfsd.delhi.dfsids
I want to derive another column which should have value of the fourth
block( out of bombay,pune,chennai,hydrabad) if third block value is
"abc"

so in this case of my expected outpout would be
derived column
-----------------------
bombay
pune
---------------------

Reply With Quote
  #2  
Old   
Leonid Gvirtz
 
Posts: n/a

Default Re: I need help on string function - 04-15-2010 , 04:39 AM






On Apr 14, 8:12*pm, sandeep <springbolt.... (AT) gmail (DOT) com> wrote:
Quote:
I have a requirement in my project

one column is getting values like

sdfjsdlj.kfjdj.abc.bombay.wue
ikekeke.kfjdj.abc.pune.uro
woeek.kfjdj.lmn.chennai.mno
oflfk.kfjdj.jky.hydrabad.xyz
eiodkk.woek.skssfsd.delhi.dfsids
I want to derive another column which should have value of the *fourth
block( out of bombay,pune,chennai,hydrabad) if third block value is
"abc"

so in this case of *my expected outpout would be
derived column
-----------------------
bombay
pune
---------------------
Hi

I think that there is no easy way to implement what you are looking
for. If your ASE version is 15.0.2 or greater, you can use a T-SQL UDF
function (see an example below). If T-SQL UDFs are not available then
you can implement the same logic using nested calls to system
functions - which is quite cumbersome.

Given said all that, I think that if extracting the n-th component
from a field is not an one-time task, you probably should consider
some redesign and store relevant string components in separate fields
from the beginning.
In addition, you can bcp out the data and it bcp the resulted file
into a new table using the dot as a delimiter.

create table tb
(
var_str varchar(100) not null
)
go

insert into tb values ('sdfjsdlj.kfjdj.abc.bombay.wue')
insert into tb values ('ikekeke.kfjdj.abc.pune.uro')
insert into tb values ('woeek.kfjdj.lmn.chennai.mno')
insert into tb values ('oflfk.kfjdj.jky.hydrabad.xyz')
insert into tb values ('eiodkk.woek.skssfsd.delhi.dfsids')
go

drop function get_delimited_component
go
create function get_delimited_component
( @p_str varchar(255),
@p_delimiter varchar(4),
@p_component_number tinyint
)
returns varchar(255)
as
begin
declare @l_delimiter_counter tinyint, @l_str varchar(255),
@l_charindex tinyint
select @l_delimiter_counter = 1, @l_str = @p_str
while @l_delimiter_counter <= @p_component_number - 1
begin
select @l_charindex = charindex(@p_delimiter, @l_str)
if @l_charindex = 0
begin
return ''
end
select @l_str = right(@l_str, char_length(@l_str) -
@l_charindex)
select @l_delimiter_counter = @l_delimiter_counter + 1
end
select @l_charindex = charindex(@p_delimiter, @l_str)
if @l_charindex = 0
begin
return @l_str
end
select @l_str = left(@l_str, @l_charindex - 1)
return @l_str
end
go

select dbo.get_delimited_component(var_str, '.', 4)
from tb
where var_str like '%.%.abc.%.%'
go

Hope it helps
Leonid Gvirtz
http://www.gvirtz-consulting.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.