-2

我需要能够输出一个字符串,该字符串将返回指定字符的所有内容,我相信使用charindex是解决此问题的最佳方法,但我不确定所需的语法。

一些例子:

  • 如果一个字符串等于"601-Test-Test2_Test3"然后我希望返回601
  • 如果一个字符串等于"42-Test_test3"然后我希望返回42
  • 如果一个字符串等于"1-Test_test3"然后我希望返回1
4

1 回答 1

1
-- this should do what you require:

DECLARE @string nvarchar(50)

SET @string = '601-Test-Test2_Test3'

SELECT @string as 'test string', left(@string, charindex('-', @string) - 1) AS 'Upto-'
于 2013-07-25T14:01:39.633 回答