-1

I have this:

21654-8012
1234-127834
12345-1222

I want to extract this:

21654
1234
12345

Basically, everything before the hyphen, - character. Does anyone have any suggestions on where to start?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425

2 Answers2

2

Use left WITH charindex() :

select t.col, left(col, charindex('-', col)-1)
from table t;
Yogesh Sharma
  • 49,081
  • 5
  • 23
  • 49
0

You can use CHARINDEX function

   DECLARE @text VARCHAR(20)
   SET @text = '123456-0000'
   SELECT SUBSTRING(@text, 0, CHARINDEX('-', @text))

Instead of @text, you can use your field name

 SELECT SUBSTRING(YOUR_COLUMN_NAME, 0, CHARINDEX('-', YOUR_COLUMN_NAME)) FROM YOUR_TABLE_NAME
Coskun Ozogul
  • 2,119
  • 1
  • 17
  • 26