rajlesta rajlesta - 2 months ago 8
SQL Question

SQL, extract integer before and after '-' of a string

I have a column with these values

row 1 Spare Part ##: **704604-001** part
row 2 Spare Part #: **511778-001**
row 3 part number, a list of common parts follows...**439730-001**)Part number Description

row 4 Spare Part: **774172-001**
Memory Board Nu
row 5 Spare Part:**639852-001**


I want to extract only the xxxxxx-001 out of the string.
example result:

704604-001
511778-001
439730-001
774172-001


Anyone can help me?

SQL 2012

Answer

if the values ​​of wanted expressions are always equal:

declare @row nvarchar(500)
set @row = 'row 1 Spare Part ##:      **704604-001** part'

select substring(@row,CHARINDEX('-',@row)-6,10)

return:

704604-001