Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select query get just the middle part only

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-09-28 : 10:01:31

declare @Sample table (Proj_name varchar(100))
insert @Sample
select 'SPIA 1 -AJ - ASESORIA JURIDICA' union all
select 'EQN 1 - DS- DERECHO SUPERFICIARIOS' union all
select 'PTB 1 - DS - DERECHO SUPERFICIARIOS' union all
select 'ENS 1 - AJ - ASESORIA JURIDICA' union all
select 'VET 1 - GS - DERECHOS SUPERFICIARIOS' union all
select 'VET 2 - GS - DERECHOS SUPERFICIARIOS' union all
select 'VET 3 - DS - DERECHOS SUPERFICIARIOS' union all
select 'VET 4 - DS - DERECHOS SUPERFICIARIOS' union all
select 'COC 1 - DS - DERECHOS SUPERFICIARIOS' union all
select 'COC 2 - AJ - ASESORIA JURIDICA' union all
select 'PEL 1 - DS - DERECHOS SUPERFICIARIOS' union all'

i want to pick project name that are in the above formats only, with two hyphens, and get just the middlepart between hyphens removing any null spaces, is it possible.

my resultset should be:

AJ
DS
GS

Lewie
Starting Member

42 Posts

Posted - 2011-09-28 : 10:40:59
select replace(left(substring(Proj_name,CHARINDEX('-',Proj_name,1) + 1,LEN(Proj_name) - CHARINDEX('-',Proj_name,1) ) ,
CHARINDEX('-',substring(Proj_name,CHARINDEX('-',Proj_name,1) + 1,LEN(Proj_name) - CHARINDEX('-',Proj_name,1))) - 1),' ','')
from @Sample
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-09-28 : 10:49:25
Lewie,

It worked, Thank you very much.
Go to Top of Page
   

- Advertisement -