all groups > sql server data warehouse > january 2004 >
You're in the

sql server data warehouse

group:

SUBSTRING AND CHARINDEX


SUBSTRING AND CHARINDEX Miky
1/15/2004 8:23:03 AM
sql server data warehouse:
Hi,

I have numerous logs like this one...using the sql
serverstring function(with Charindex) or any other
function, can someone help me and tell me how do i pull
out the 1st value after the symbol '&' (symbol ampersand)?
So, i want to split this one field of char into 5 or 6
different fields which will return me the
values 'V', 'U', 'C', 'A', etc...
Basically, I want to extract each uppercase letter
immediately following the ampersand symbol"&" from this
one column containing the following type of data...


V=1&U=20d4db13003b6d4bb6a3ffa89aa0ecaf&C=au&A=w&I=winxp.win
dowsxp.ver_platform_win32_nt.5.1.x86.en.ver_nt_workstation.
..2600.1.0.com_microsoft.q817287_xp_sp2.&D=&P=5.1.a28.2.100.
1.0&L=en-US&S=s&E=00000000&M=&X=030801023903796


Thanks in advance,

Re: SUBSTRING AND CHARINDEX Vishal Parkar
1/16/2004 7:24:31 AM
hi Miky,

This is not possible using standard SQL, you will have to write t-sql for
that.
Ex:

-- sample table
drop table #t
create table #t (c2 int, c1 varchar(500), upd_column varchar(50))
-- sample data
insert into #t values (1,
'V=1&U=20d4db13003b6d4bb6a3ffa89aa0ecaf&C=au&A=w&I=winxp.win', null)
insert into #t values (2, '&X',null)

--im updating upd_column with the required values.

declare @x varchar(100)
declare @c2 int
declare @pos int
set @pos = 0
set @c2 = 0
update #t set upd_column = ''
while @c2 is not null
begin
select @c2=min(c2) from #t where c2 > @c2
select @x=c1 from #t where c2=@c2
while 1=1
begin
select @pos=charindex('&', @x, @pos + 1)
if @pos = 0
break
update #t set upd_column = upd_column + substring (@x, @pos + 1, 1) where c2
= @c2
end
end
select * from #t



-- Vishal


AddThis Social Bookmark Button