-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
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

3

Use left WITH charindex() :

select t.col, left(col, charindex('-', col)-1)
from table t;
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
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,389
  • 1
  • 20
  • 32