Skip to content
Advertisement

SQL syntax alphanumeric characters, SQL Server

If I pull this ID down from my source system it looks like 9006ABCD.

What would the syntax look like if I just want to return 9006 as the ID?

Essentially, I don’t need the alpha characters.

Advertisement

Answer

Assuming that '9006ABCD' is a string value, then you can extract the leading numbers using:

select left(id, patindex('%[^0-9]%', id + 'X') - 1)

Of course, there may be easier ways. If you just want the first four characters, then use left(id, 4).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement