在 SQL Server 2008 中的符号之前隔离字符串中的子字符串 [英] isolating a sub-string in a string before a symbol in SQL Server 2008
本文介绍了在 SQL Server 2008 中的符号之前隔离字符串中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从如下所示的字符串中提取子字符串(在本例中为连字符之前的所有内容):
i am trying to extract a substring(everything before a hyphen, in this case) from a string as shown below:
Net Operating Loss - 2007
Capital Loss - 1991
Foreign Tax Credit - 1997
并希望使用 SQL Server Management Studio 2008 分别使用年份和名称(连字符前的子字符串).有什么建议吗?或者想知道如何实现这一目标?
and want the year and name(substring before hyphen) separately, using SQL server Management studio 2008. Any advice? or idea how i can achieve this?
推荐答案
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
这篇关于在 SQL Server 2008 中的符号之前隔离字符串中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文