-2

I need to be able to output a string which will return everthing up a specified character, I believe making use of charindex is the best way to approach this but I am unsure of the syntax required.

Some examples:

  • If a string is equal to "601-Test-Test2_Test3" then I wish to return 601
  • If a string is equal to "42-Test_test3" then I wish to return 42
  • If a string is equal to "1-Test_test3" then I wish to return 1
JsonStatham
  • 9,770
  • 27
  • 100
  • 181

1 Answers1

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-'
JsonStatham
  • 9,770
  • 27
  • 100
  • 181