sql-server – Pull data between MS SQL characters

Question:

Good day!

There is a need to split the string into components, namely how to pull out the phone number following the comma ('8555555555, 8999999999, 9166666666, etc.')

How to do it?

There can be either one or fifteen phones, and you need to pull out all phone numbers from the maximum possible after the decimal point))

My experience:

 SELECT '8555555555, 8999999999, 9166666666' as [All NUM]
        ,SUBSTRING('8555555555, 8999999999, 9166666666', 1, charindex(',', '8555555555, 8999999999, 9166666666')-1) as [First NUM]
        ,SUBSTRING('8555555555, 8999999999, 9166666666', charindex(',', '8555555555, 8999999999, 9166666666')+1, LEN('8555555555, 8999999999, 9166666666') - charindex(',', '8555555555, 8999999999, 9166666666')) as [All after first NUM]
        ,right('8555555555, 8999999999, 9166666666',charindex(',',  SUBSTRING('8555555555, 8999999999, 9166666666', charindex(',', '8555555555, 8999999999, 9166666666')+1, LEN('8555555555, 8999999999, 9166666666') - charindex(',', '8555555555, 8999999999, 9166666666')))-2) as [Last NUM]

Answer:

Apply the SQL Server function string_split – of type:

 SELECT value FROM STRING_SPLIT('8555555555, 8999999999, 9166666666', ', ')
Scroll to Top