0

我需要得到-和·之间的字符串,即GigabitEthernet1/0/1

CW-3D13-SW1 - GigabitEthernet1/0/1 · Uplink to 1K5-Core1
CW-3D13-SW1 - FastEthernet1/0/43 · PHSA-MPAACT-3D13/16 - Cisco 2811 Fa 0/0
c&w-internet-sw-ACB - GigabitEthernet1/0/24 · MPAACT PNG/UBC School of Medicine
c&w-internet-sw-ACB - GigabitEthernet1/0/25 - Int-Link-CW-BCCA-Oak-St

我可以使用以下

SUBSTRING(Caption, CHARINDEX(' - ', Caption) + 3, CHARINDEX(' · ', Caption) - CHARINDEX('- ', Caption) + LEN(' · ') - 3)

它会给我我想要的东西,但在某些情况下, · 会被 - 代替。例如:

c&w-internet-sw-ACB - GigabitEthernet1/0/25 - Int-Link-CW-BCCA-Oak-St

这会打断我的查询。有没有办法同时满足 - 和 ·

感谢

更新 - 是否可以添加另一个条件?当字符串如下:

VPN 3030  - PCI Fast Ethernet  

最后没有-或·。在这种情况下,我只想要 -

4

3 回答 3

4

Try this

Declare @LeftMarker varchar(3)
Declare @RightMarker varchar(3)
    Set @LeftMarker = ' - '     --<--- Replace this with your choice
    Set @RightMarker = ' · '    --<--- Replace this with your choice

Declare @LMarkerLen int
    Set @LMarkerLen = LEN(@LeftMarker)

SELECT  Case When CHARINDEX(@RightMarker, Caption) > 0 Then
            SUBSTRING(Caption, CHARINDEX(@LeftMarker, Caption) + @LMarkerLen, CHARINDEX(@RightMarker, Caption) - (CHARINDEX(@LeftMarker, Caption) + @LMarkerLen))
        Else
            SUBSTRING(Caption, CHARINDEX(@LeftMarker, Caption) + @LMarkerLen, CHARINDEX(@LeftMarker, Caption, CHARINDEX(@LeftMarker, Caption) + @LMarkerLen) - (CHARINDEX(@LeftMarker, Caption) + @LMarkerLen))
        End AS Result
FROM .......
于 2014-01-08T00:56:43.060 回答
0

我为此创建了一个 min 函数。

create function dbo.fnMin( @P1 int, @P2 int)
    returns int
as
begin
    return case when @P1 > @P2 then coalesce(@P2,@P1) else coalesce(@P1,@P2) end;
end
go

它只返回两个值中较小的一个。然后你可以用它来做RIGHT后跟LEFT。在我看来,您实际上可以将它放在一个单独的 select 语句中,但按照我的方式拆分它会使其更具可读性。试一试...希望它有效!

select coalesce(left(SubStr, dbo.fnMin(nullif(charindex(' - ',SubStr),0),nullif(charindex(' · ',SubStr),0)) ),SubStr)
from (
    select right([Caption],len([Caption])-(dbo.fnMin(nullif(charindex(' - ',[Caption]),0),nullif(charindex(' · ',[Caption]),0)))-3) as SubStr
    from <YourTableName>
) t

内部 select 语句获取第一个“ - ”或“ · ”的所有值RIGHT。然后外部选择将获得第二个“ - ”或“ · ” 的所有内容我已经编辑了我的代码以不考虑第二个“-”或“·”


编辑:我不能使用 sqlfiddle,因为这个解决方案需要一个函数......但这是我能够在我的沙盒环境中运行的......

create table dbo.Interfaces (Caption varchar(1000))
go
insert into dbo.Interfaces
values
('CW-3D13-SW1 - GigabitEthernet1/0/1 · Uplink to 1K5-Core1'),
('CW-3D13-SW1 - FastEthernet1/0/43 · PHSA-MPAACT-3D13/16 - Cisco 2811 Fa 0/0'),
('c&w-internet-sw-ACB - GigabitEthernet1/0/24 · MPAACT PNG/UBC School of Medicine'),
('c&w-internet-sw-ACB - GigabitEthernet1/0/25 - Int-Link-CW-BCCA-Oak-St'),
('VPN 3030  - PCI Fast Ethernet')
go

create function dbo.fnMin( @P1 int, @P2 int)
    returns int
as
begin
    return case when @P1 > @P2 then coalesce(@P2,@P1) else coalesce(@P1,@P2) end;
end
go

select coalesce(left(SubStr, dbo.fnMin(nullif(charindex(' - ',SubStr),0),nullif(charindex(' · ',SubStr),0)) ),SubStr)
from (
    select right([Caption],len([Caption])-(dbo.fnMin(nullif(charindex(' - ',[Caption]),0),nullif(charindex(' · ',[Caption]),0)))-3) as SubStr
    from dbo.Interfaces
) t
go

这是结果......

igabitEthernet1/0/1 
astEthernet1/0/43 
igabitEthernet1/0/24 
igabitEthernet1/0/25 
CI Fast Ethernet

(5 row(s) affected)

如果您无法从这段代码中弄清楚......是时候打书先生了。;)

于 2014-01-09T21:21:10.927 回答
0

它很丑陋,但我认为它可以满足您的需求。

SELECT 
  SUBSTRING(Caption, 
            CHARINDEX(' - ', Caption) + 3, 
            CHARINDEX(' · ', Caption) - CHARINDEX('- ', Caption) + LEN(' · ') - 3) 
FROM
  Foo
WHERE
  CHARINDEX(' · ', Caption) > 0
UNION
SELECT
SUBSTRING(
  REVERSE(
  SUBSTRING(REVERSE(Caption), 
            CHARINDEX(' - ', REVERSE(Caption)) + 3,
            LEN(Caption))
  ),
  CHARINDEX(' - ', REVERSE(
  SUBSTRING(REVERSE(Caption), 
            CHARINDEX(' - ', REVERSE(Caption)) + 3,
            LEN(Caption))
  )) + 3,
  LEN(Caption))
FROM
  Foo
WHERE
  CHARINDEX(' · ', Caption) = 0

简单地解释一下,对于那些点不是分隔符/定界符的人,反转标题并从“ - ”的第一个实例一直到最后获取一个子字符串。反转此操作的结果,将其返回到原来的顺序,并再次获取从“ - ”的第一个实例到结尾的子字符串。

于 2014-01-08T00:52:43.977 回答