提取' - '左侧的文字。

时间:2016-06-18 22:20:55

标签: sql sql-server

我正在尝试将数据放在左侧 - 我尝试了下面的代码

LocationName
CCC-CC36-A
5N-N577-A
5N-N577-A
5N-N577-A
2T-225-A
PE-020-A

select 
LEFT(LocationName, LEN(LocationName) - CHARINDEX('-',LocationName)-1),
EncounterNumber
from #TubeRateQty

2 个答案:

答案 0 :(得分:2)

SELECT 
  LEFT(LocationName, ISNULL(NULLIF(CHARINDEX('-',LocationName),0), LEN(LocationName)+1)-1)
 ,EncounterNumber
from #TubeRateQty

答案 1 :(得分:0)

DECLARE @xml xml

SELECT @xml =(
    SELECT CAST('<i><a>' + REPLACE(LocationName,'-','</a><a>') + '</a></i>' as xml)
    FROM YourTable
    FOR XML PATH('')
)

SELECT  t.v.value('a[1]','nvarchar(100)'),
        t.v.value('a[2]','nvarchar(100)'),
        t.v.value('a[3]','nvarchar(100)')
FROM @xml.nodes('/i') as t(v)

输出:

CCC        CC36       A
5N         N577       A
5N         N577       A
5N         N577       A
2T         225        A
PE         020        A