In this post, we are going to learn how to extract the first numeric value from an alphanumeric string in SQL Server. To demonstrate this, we will create a dummy table and then we will insert some dummy rows into that table. Finally, we will use a SELECT statement to extract the first numeric value from the given alphanumeric string for each row of the table.
Let’s create the dummy table as below:
Now, let’s insert some alphanumeric values in this table as below:
We have below sample records in the testTable:
Now, to extract the first numeric value for each row of the table, we can use a SELECT query as below:
Below is the output:
Understanding the query
To extract the first number from the given alphanumeric string, we are using a SUBSTRING function. In the substring function, we are extracting a substring from the given string starting … More