Blog Post
SQL Server - Extracting All The Words From a String In An SQL Query
Author
Mandy Doward
Date
November 11, 2016
Length
1 min
Extracting All The Words From a String In A SQL Server Database Query
It is easy enough to extract the first word from a string in a database query, but what if you need to separate all words in a given string into a list of separate words?
This is a job for Common Table Expressions (CTEs) as they enable us to repeatedly read a single record while changing it each time.
Here is a SQL Server example which takes a string variable and spearates it into inidividual words. We use a combination of CHARINDEX, LEFT and RIGHT functions to achieve it.
The following screen shot shows the results from the query:
The following version would extract just the last word of thre given string:
These queries could be adapted to query a character column from a set of database table records.
In a later blog I will show you how to create a dictionary word list from column values in a table.
You can find out more about database querying by attending our SQL Server Database Querying Training Courses and Transact SQL programming Courses.
Share This Page
Mandy Doward
Managing Director
PTR’s owner and Managing Director is a Microsoft MCSE certified Business Intelligence (BI) Consultant, with over 30 years of experience working with data analytics and BI.
PTR FAQs
See our FAQs or get in contact for more information on any of our services, solutions, or to start your PTR journey.