Split Alphabets From Alphanumeric String In SQL Server

Introduction

 
This article gives an explanation about how to split alphabets from the alphanumeric string in an SQL server. Here I'll also explain how to create a function in an SQL server.
 
In my previous article, I explained what is an alphanumeric string and how to split numbers from an alphanumeric string in an SQL server that you might like to read.
 
While working with any data-driven applications, sometimes we need to split the numbers and alphabets from the input string as per the given requirement. I got many emails from students and beginner programmers to write an article on ways to get only varchar values from the string in the SQL server. So today in this article I'll explain how to archieve this requirement to split the numbers and alphabets and return only varchar value from the string.
 
Requirement
  1. How to get alphabets from an alphanumeric string in SQL Server?

Implementation

 
So, let's create a query to split the alphabets from the string in the SQL server.
 
Get Alphabets from string
 
Let's split the alphanumeric string and get only alphabets from the string. So, we will take an example for demonstration.
 
I have my enrollment number, which is a combination of numbers and alphabets, and I want only alphabets from my enrollment number.
 
Example
  1. Input (Enrollment Number): SOE14CE13017  
  2. Expected Output: SOECE  
SQL Query to Get Alphabets From String
  1. DECLARE @strEnrollmentNumber NVARCHAR(MAX) = 'SOE14CE13017'  
  2. DECLARE @intNumber INT    
  3.    
  4. SET @intNumber = PATINDEX('%[^A-Za-z]%', @strEnrollmentNumber)    
  5.    
  6. WHILE @intNumber > 0    
  7.   BEGIN  
  8.     SET @strEnrollmentNumber = STUFF(@strEnrollmentNumber, @intNumber, 1, '' )    
  9.     SET @intNumber = PATINDEX('%[^A-Za-z]%', @strEnrollmentNumber )    
  10. END  
Explanation
 
As you can see in the query above, here, we have declared two different temp variables @strEnrollmentNumber which indicates an Input string, and @intNumber that is taken to check whether the input string contains a number or not. Then using the PATINDEX function of the SQL server we have identified that the string input string contains a number or not and stored the return value of this function into @intNumber.
 
In SQL server PATINDEX is a function that accepts search pattern and expression(input string) as a parameter and returns, starting position of the first occurrence of the pattern in a specified expression(input string), PATINDEX will return 0 if the pattern is not found in the specified expression(input string). Here, we have used a pattern '%[^A-Za-z]%' that indicates only alphabets from a to z and A to Z.
 
Now, by using the while loop in the SQL server we removed the numbers from the input string which not match with the given pattern '%[^A-Za-z]%' one by one using the STUFF function and store the result in the @strEnrollmentNumber variable and again set the value of @intNumber as per the specified pattern '%[^A-Za-z]%' as we used condition @intNumber > 0 in while loop, So it will do the same process again and again and remove numbers from the input string one by one till @intNumber gets 0 and remove all the numbers from the input string.
 
In SQL Server STUFF() function is used to deletes a specified sequence of characters from a source/Input string and then inserts another set of sequence of characters at a specified starting point. I have written an article on STUFF() function with syntax and examples that you might like to read.
 
Use of Query
  1. SELECT @strEnrollmentNumber  
Output
  1. SOECE 
You also can create a function to get only alphabets from the input string to reduce the complexity of the query.
 
Function to Get Alphabets From String
  1. CREATE FUNCTION [dbo].[GetAlphabetsFromString]  
  2. (  
  3.     @strInputString  VARCHAR(MAX)  
  4. )    
  5. RETURNS VARCHAR(MAX)  
  6. AS    
  7. BEGIN    
  8.     DECLARE @intValue INT    
  9.     SET @intValue = PATINDEX('%[^A-Za-z]%', @strInputString)    
  10. BEGIN    
  11.     WHILE @intValue > 0    
  12.     BEGIN    
  13.         SET @strInputString = STUFF(@strInputString, @intValue, 1, '' )    
  14.         SET @intValue = PATINDEX('%[^A-Za-z]%', @strInputString )    
  15.     END    
  16. END    
  17.     RETURN ISNULL(@strInputString,'')    
  18. END    
  19. GO  
Use of Function
  1. SELECT dbo.GetAlphabetsFromString('SOE14CE13017')  
Output
  1. SOECE  
Explanation
 
As you can see in the created function above here we have created a function that accepts inputString as an argument and all the logic is the same as I explained in the above SQL query to return only alphabets from the string. Finally, this function returns the varchar value from the string and if the input string dose does not contain any alphabets then this function will return an empty string.
 

Summary

 
In this article, we learned how to split the alphabets from the alphanumeric string in the SQL server as well as about the PATINDEX() and STUFF() function of the SQL server and way create a function in the SQL server that returns a varchar value.
 
Reference LInk