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.
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
- 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
- Input (Enrollment Number): SOE14CE13017
- Expected Output: SOECE
SQL Query to Get Alphabets From String
- DECLARE @strEnrollmentNumber NVARCHAR(MAX) = 'SOE14CE13017'
- DECLARE @intNumber INT
-
- SET @intNumber = PATINDEX('%[^A-Za-z]%', @strEnrollmentNumber)
-
- WHILE @intNumber > 0
- BEGIN
- SET @strEnrollmentNumber = STUFF(@strEnrollmentNumber, @intNumber, 1, '' )
- SET @intNumber = PATINDEX('%[^A-Za-z]%', @strEnrollmentNumber )
- 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
- SELECT @strEnrollmentNumber
Output
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
- CREATE FUNCTION [dbo].[GetAlphabetsFromString]
- (
- @strInputString VARCHAR(MAX)
- )
- RETURNS VARCHAR(MAX)
- AS
- BEGIN
- DECLARE @intValue INT
- SET @intValue = PATINDEX('%[^A-Za-z]%', @strInputString)
- BEGIN
- WHILE @intValue > 0
- BEGIN
- SET @strInputString = STUFF(@strInputString, @intValue, 1, '' )
- SET @intValue = PATINDEX('%[^A-Za-z]%', @strInputString )
- END
- END
- RETURN ISNULL(@strInputString,'')
- END
- GO
Use of Function
- SELECT dbo.GetAlphabetsFromString('SOE14CE13017')
Output
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