So, I decided to post an example-based article that discusses some simple ways of segmenting strings with T-SQL mainly by using the SUBSTRING built-in function and some other built-in functions such as CHARINDEX, LEFT, RIGHT and LEN.
Additionally, in one of my previous posts, I described the basic String built-in functions in SQL Server.
Enough theory, let’s see some examples!
Requirement 1: Get the first word only
Requirement 2: Get the second word only
Requirement 3: Get the first letters of each word
— We also need to use the CHARINDEX, LEN and LEFT built-in functions.
This post provided simple examples on how strings can be segmented in SQL Server. I did not get into complex scenarios as if I did that I am sure that I would need many hours for finishing the article as the scenarios can vary a lot!
If you have another string segmentation scenario and/or its solution feel free to add it by commenting this post!
I hope you found this post useful!
Artemakis Artemiou is a Senior SQL Server Architect, Author, and a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and TechHowTos.com. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA).