Как насчет встроенной функции, которая принимает строку, разделитель и какую часть вы хотите вернуть
Create Function dbo.GetPart(@InString as varchar(1000)
, @Part as int
, @Delim as varchar(10))
Returns varchar(1000) as
Begin
Declare @CurrentPart int
Declare @i int
Declare @j int
Declare @Ret varchar(1000)
Set @Ret = ''
Set @i = 0
Set @InString = Replace(@InString, ' ', '')
Set @CurrentPart = 1
while (@CurrentPart <= @Part)
Begin
Set @j = charindex(@Delim, @InString, @i + 1 )
if @j = 0 set @j = len(@InString) + 1
if ((@j - @i) > 0 and @CurrentPart = @Part)
Begin
Set @Ret = Substring(@InString, @i , @j - @i)
If @Ret = '' set @ret = 'Weird'
break
End
Set @i = charindex(@Delim, @InString, @i) + len(@delim)
Set @CurrentPart = @CurrentPart + 1
End
if @Ret = '' Set @Ret = 'inconveivable'
Return @Ret
End
GO
Select dbo.GetPart('DB1 - DB2 - DB3',1, '-') as Field1
,dbo.GetPart('DB1 - DB2 - DB3',2, '-') as Field2
, dbo.GetPart('DB1 - DB2 - DB3',3, '-') as Field3
Select dbo.GetPart('DataBit1 - DataBit2 - DataBit3',1, '-') as Field1
,dbo.GetPart('DataBit1 - DataBit2 - DataBit3',2, '-') as Field2
, dbo.GetPart('DataBit1 - DataBit2 - DataBit3',3, '-') as Field3