Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract part of a string in t-sql

Tags:

If I have the following nvarchar variable - BTA200, how can I extract just the BTA from it?

Also, if I have varying lengths such as BTA50, BTA030, how can I extract just the numeric part?

like image 390
Xaisoft Avatar asked Dec 17 '08 16:12

Xaisoft


People also ask

How do I select part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

Which function is used to extract a part of the string in SQL?

SUBSTRING function in SQL queries The SUBSTRING() function extracts the substring from the specified string based on the specified location. In the below example, we retrieve a substring using the specified inputs. We can understand the substring output using the following image.

How do I get the first 4 characters of a string in SQL?

SQL Server LEFT() Function The LEFT() function extracts a number of characters from a string (starting from left).


2 Answers

I would recommend a combination of PatIndex and Left. Carefully constructed, you can write a query that always works, no matter what your data looks like.

Ex:

Declare @Temp Table(Data VarChar(20))

Insert Into @Temp Values('BTA200')
Insert Into @Temp Values('BTA50')
Insert Into @Temp Values('BTA030')
Insert Into @Temp Values('BTA')
Insert Into @Temp Values('123')
Insert Into @Temp Values('X999')

Select Data, Left(Data, PatIndex('%[0-9]%', Data + '1') - 1)
From   @Temp

PatIndex will look for the first character that falls in the range of 0-9, and return it's character position, which you can use with the LEFT function to extract the correct data. Note that PatIndex is actually using Data + '1'. This protects us from data where there are no numbers found. If there are no numbers, PatIndex would return 0. In this case, the LEFT function would error because we are using Left(Data, PatIndex - 1). When PatIndex returns 0, we would end up with Left(Data, -1) which returns an error.

There are still ways this can fail. For a full explanation, I encourage you to read:

Extracting numbers with SQL Server

That article shows how to get numbers out of a string. In your case, you want to get alpha characters instead. However, the process is similar enough that you can probably learn something useful out of it.

like image 129
George Mastros Avatar answered Sep 30 '22 21:09

George Mastros


substring(field, 1,3) will work on your examples.

select substring(field, 1,3) from table

Also, if the alphabetic part is of variable length, you can do this to extract the alphabetic part:

select substring(field, 1, PATINDEX('%[1234567890]%', field) -1) 
from table
where PATINDEX('%[1234567890]%', field) > 0
like image 21
Stanislas Biron Avatar answered Sep 30 '22 21:09

Stanislas Biron