在SQL Server 2008中的符号前隔离字符串中的子串(isolating a sub-str

2019-07-30 14:45发布

我试图抽取子从字符串(连字符之前的一切,在这种情况下),如下图所示:

Net Operating Loss - 2007
Capital Loss - 1991
Foreign Tax Credit - 1997

并希望在今年和名称(连字符前子)分开,使用SQL Server Management Studio 2008中的任何意见? 或想法如何,我可以做到这一点?

Answer 1:

DECLARE @test nvarchar(100)

SET @test = 'Foreign Tax Credit - 1997'

SELECT @test, left(@test, charindex('-', @test) - 2) AS LeftString,
    right(@test, len(@test) - charindex('-', @test) - 1)  AS RightString


Answer 2:

DECLARE @dd VARCAHR(200) = 'Net Operating Loss - 2007';

SELECT SUBSTRING(@dd, 1, CHARINDEX('-', @dd) -1) F1,
       SUBSTRING(@dd, CHARINDEX('-', @dd) +1, LEN(@dd)) F2


文章来源: isolating a sub-string in a string before a symbol in SQL Server 2008