Friday, March 23, 2012

need some TSQL help

I have some entries in a column as such
XYZ ( ABC ) ( 1)
XYZ ( ABC ) ( 11)
XYZ ( ABC ) ( 2)
XYZ ( ABC ) ( 3)
XYZ ( ABC ) ( 4)
UVW ( XYZ ) ( 10)
UVW ( XYZ ) ( 12)
UVW ( XYZ ) ( 41)
So i want to group entries like this by the first ')' seen
so I can get distinct values such as
XYZ ( ABC )
UVW ( XYZ )
How can i write the query ? Thankscreate table t (
s varchar(40)
)
go
insert into t values ('XYZ ( ABC ) ( 1)')
insert into t values ('XYZ ( ABC ) ( 11)')
insert into t values ('XYZ ( ABC ) ( 2)')
insert into t values ('XYZ ( ABC ) ( 3)')
insert into t values ('XYZ ( ABC ) ( 4)')
insert into t values ('UVW ( XYZ ) ( 10)')
insert into t values ('UVW ( XYZ ) ( 12)')
insert into t values ('UVW ( XYZ ) ( 41)')
go
select distinct
substring(s,1,charindex(')',s)) as s
from t
where charindex(')',s) > 0
go
drop table t
Steve Kass
Drew University
Hassan wrote:

>I have some entries in a column as such
>XYZ ( ABC ) ( 1)
>XYZ ( ABC ) ( 11)
>XYZ ( ABC ) ( 2)
>XYZ ( ABC ) ( 3)
>XYZ ( ABC ) ( 4)
>UVW ( XYZ ) ( 10)
>UVW ( XYZ ) ( 12)
>UVW ( XYZ ) ( 41)
>So i want to group entries like this by the first ')' seen
>so I can get distinct values such as
>XYZ ( ABC )
>UVW ( XYZ )
>How can i write the query ? Thanks
>
>|||or
select substring(s,1,charindex(')',s)) as s
from #t
where charindex(')',s) > 0
group by substring(s,1,charindex(')',s))
Madhivanan

No comments:

Post a Comment