Split Delimited String into Columns in SQL Server with PARSENAME

By:   |   Comments (18)   |   Related: 1 | 2 | 3 | 4 | 5 | 6 | > Functions System


Problem

Typically, in SQL Server data tables and views, values such as a person's name or their address is stored in either a concatenated string or as individual columns for each part of the whole value. For example: John Smith 123 Happy St Labor Town, CA. This data could be stored in a single column with all the data, in two columns separating the person's name from their address or in multiple columns with a column for each piece of the total value of the data.

With this, DBA's are inevitably always having to concatenate or parse the values to suit our customer's needs.

To build on our sample from above, I have an address column that has the full address (street number and name, city and state) separated by commas in a concatenated column. I want to break that data into three columns (street, city and state) respectively.

Solution

For this tip, let's create a test database and test table for the parsing example. In this scenario, we have a street name and number, followed by the city, then the state.

USE master;
GO

CREATE DATABASE myTestDB;
GO

USE myTestDB;
GO

The following code creates a table in the new (test) database with only two columns - an id column and an address column.

CREATE TABLE dbo.custAddress(
     colID INT IDENTITY PRIMARY KEY
   , myAddress VARCHAR(200)
   );
GO

Populate the table with some generic data. Notice that the address is one string value in one column.

INSERT INTO dbo.custAddress(myAddress)
VALUES('7890 – 20th Ave E Apt 2A, Seattle, VA')
    , ('9012 W Capital Way, Tacoma, CA')
    , ('5678 Old Redmond Rd, Fletcher, OK')
    , ('3456 Coventry House Miner Rd, Richmond, TX')
GO

Confirm the table creation and data entry with the following SQL SELECT statement.

SELECT *
FROM dbo.custAddress;
GO

Your results should return, as expected, two columns - the id column and the address column. Notice the address column is returned as a single string value in image 1.

query results

Image 1

Breaking down the data into individual columns. The next step will be to parse out the three individual parts of the address that are separated by a comma.

SELECT 
     REVERSE(PARSENAME(REPLACE(REVERSE(myAddress), ',', '.'), 1)) AS [Street]
   , REVERSE(PARSENAME(REPLACE(REVERSE(myAddress), ',', '.'), 2)) AS [City]
   , REVERSE(PARSENAME(REPLACE(REVERSE(myAddress), ',', '.'), 3)) AS [State]
FROM dbo.custAddress;
GO

Since we didn't call the id column in the SELECT statement above, the results only returned the address string divided into three columns, each column returning a portion of the address for the respective column as shown in image 2.

query results

Image 2

Here in the real world, DBA's are often faced with more complex tables or views and not just a simple two column table as in the above sample. Although the sample above is a great primer for dissecting how to parse a string value, the following section demonstrates a more complex situation.

Again, create a sample table in our test database to work with. Here, we create a slightly more complex table with some additional columns.

CREATE TABLE employeeData(
     colID INT IDENTITY PRIMARY KEY
   , empID INT
   , empName VARCHAR(50)
   , empAddress VARCHAR(200)
   , empPhone VARCHAR(12)
   , jobClass VARCHAR(50)
   );
GO

Insert some generic data into the test table.

INSERT INTO employeeData(empID, empName, empAddress, empPhone, jobClass)
VALUES (1, 'John, M, Smith, Jr', '123 Happy Hollow, BarnYard, OK, 90294', '202-555-0118', 'Programmer')
     , (2, 'Joe, S, Jones, Sr', '456 Sad Ln, BarnDoor, TX, 90295', '202-555-0195', 'Tester')
     , (3, 'Sammy, L, Smuthers', '5655 Medow Lane, Pastuer, CA, 90296', '202-555-0192', 'Sales') 
     , (4, 'Henry, R, Lakes, Esq', '8749 Sunshine Park, Glenndale, HA, 90297', '202-555-0141', 'Manager')
     , (5, 'Harry, Q, Public, Jr', '555 Somber Ln, Levy, OR, 90298', '202-555-0137', 'Graphical Designer')
GO

Now, let's create a mixed SELECT statement to pull all the columns that breaks down the "empName", "empAddress" and "empPhone" columns into multiple columns.

In the following block of code, notice that I restarted my "place / position" count on each column that I want to parse. Each time you start parsing a new column, you must start your count over. You should also note that the "empName" and "empAddress" string values are separated with a comma while the "empPhone" string value is separated by a hyphen and thus should be reflected in your "parse" function between the first set of single quotes.

SELECT 
   colID
   , empID
   -- The following section breaks down the "empName" column into three columns.
   , REVERSE(PARSENAME(REPLACE(REVERSE(empName), ',', '.'), 1)) AS FirstName
   , REVERSE(PARSENAME(REPLACE(REVERSE(empName), ',', '.'), 2)) AS MiddleName
   , REVERSE(PARSENAME(REPLACE(REVERSE(empName), ',', '.'), 3)) AS LastName
   -- The following section breaks down the "empAddress" column into four columns.
   , REVERSE(PARSENAME(REPLACE(REVERSE(empAddress), ',', '.'), 1)) AS Street
   , REVERSE(PARSENAME(REPLACE(REVERSE(empAddress), ',', '.'), 2)) AS City
   , REVERSE(PARSENAME(REPLACE(REVERSE(empAddress), ',', '.'), 3)) AS State
   , REVERSE(PARSENAME(REPLACE(REVERSE(empAddress), ',', '.'), 4)) AS ZipCode
   -- The following section breaks down the "empPhone" column into three columns
   , REVERSE(PARSENAME(REPLACE(REVERSE(empPhone), '-', '.'), 1)) AS AreaCode
   , REVERSE(PARSENAME(REPLACE(REVERSE(empPhone), '-', '.'), 2)) AS Prefix
   , REVERSE(PARSENAME(REPLACE(REVERSE(empPhone), '-', '.'), 3)) AS LastFour
   , jobClass
FROM employeeData;
GO

The results should return thirteen columns from the six columns queried against as shown in image 3.

query results

Image 3

In summary, the PARSENAME function is a handy addition to your T-SQL toolkit for writing queries involving delimited data. It allows for parsing out and returning individual segments of a string value into separate columns. Since the PARSENAME function breaks down the string, you are not obligated to return all the delimited values. As in our sample above, you could have returned only the area code from the "empPhone" column to filter certain area codes in your search.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Aubrey Love Aubrey Love is a self-taught DBA with more than six years of experience designing, creating, and monitoring SQL Server databases as a DBA/Business Intelligence Specialist. Certificates include MCSA, A+, Linux+, and Google Map Tools with 40+ years in the computer industry. Aubrey first started working on PC’s when they were introduced to the public in the late 70's.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Friday, December 9, 2022 - 2:09:53 PM - Greg Robidoux Back To Top (90755)
Hi John,

see if this works for your needs: https://www.mssqltips.com/sqlservertip/6390/sql-server-split-string-replacement-code-with-stringsplit/

-Greg

Friday, December 9, 2022 - 1:31:53 PM - John Bolduc Back To Top (90754)
This is great, except I can't get it to work if there are 4-5 periods in the text string. I have a text string that can contain 0-5 periods, separating the string into up to 6 parts. Your solution works great if there are 0-3 periods (1-4 parts), but if there are 4-5 periods (5-6 parts) I get returned for ALL 6 parts of those strings. I can't figure out why. It's not-related to string length, as some of the 3 or 4-part strings are longer than some of the 5 or 6-part strings.

Friday, October 7, 2022 - 5:57:33 AM - Hristo Hristov Back To Top (90567)
Great article! For me this method does not work because I have decimal values in the string to be split up, so I will be splitting the whole number from the decimal part. I solved the challenge with a loop that I implemented on a trigger. The trigger is attached to a drop table and fires on insert. The target value is split and parsed into the respective columns in a curated table. Here is my code: https://github.com/hristochr/SQL-Playground/blob/master/split_by_delimiter_with_trigger.sql

Tuesday, September 20, 2022 - 4:15:26 PM - Aubrey Back To Top (90501)
Warren,

You are very welcome.
I love it when someone benefits from my articles. That's what they are here for.
Thanks for posting a comment.

Tuesday, September 20, 2022 - 5:50:13 AM - Warren Back To Top (90500)
Thank you for this article.... it helped 1000%

Monday, February 7, 2022 - 1:57:38 PM - Jeff Moden Back To Top (89762)
Nice article, Aubrey. There are a couple of big pieces missing, though.

One of the pieces with two possible serious problems is that the return is of the SYSNAME datatype. That means two things:
1. The elements within a string must be less that 128 characters. That's usually not a problem but if someone is expecting to be able to split out longer elements, it's not going to work.
2. The results are the equivalent of NVARCHAR(128) and if you use any of those values as criteria during a lookup, you may run into serious datatype mismatch issues that result in full table scans.

As a sidebar, names and addresses frequently contain periods and those will need to be replaced by another character first and maybe put back later.

Monday, January 31, 2022 - 10:31:20 PM - Chirag Rawal Back To Top (89718)
The only issue with using this method is when you have a string with more than 4 values in it. For example "Name, Address, Phone, Email, Income". This method would not work, as PARSENAME method can only be used till 4 levels. A generic CharIndex would be a more sustainable option.

Wednesday, July 7, 2021 - 2:47:15 PM - Aubrey Back To Top (88962)
Thanks for the comment Alex, I'm glad it worked for you.
I try to make all my code examples work out-of-the-box with just a simple copy/paste.

Wednesday, July 7, 2021 - 7:14:07 AM - Alex Back To Top (88956)
Works fine just by C&P. I still learning, but had the question to separate a column containing numbers as text (07.2021) for month and year into separate colums one for month, one for year. Exactly what i needed - simple and useful.

Thursday, March 18, 2021 - 11:43:25 AM - Mike Back To Top (88437)
It might lessen some confusion if image 1 and the preceding data insert statements agree. Your insertion of data has no '.' in the data, while the image clearly shows '.' in the data.

Thursday, March 18, 2021 - 10:07:20 AM - Paul Hunter Back To Top (88436)
Would this be an opportunity to use CROSS APPLY to perform the reverse & replace on the column(s) once so they can then be parsed and reversed? It seems like a solution like this would be less CPU intensive.

SELECT
REVERSE(PARSENAME(ar.AddressRev, 1)) AS [Street]
, REVERSE(PARSENAME(ar.AddressRev, 2)) AS [City]
, REVERSE(PARSENAME(ar.AddressRev, 3)) AS [State]
FROM dbo.custAddress ca
cross apply ( select REPLACE(REVERSE(myAddress), ',', '.') as AddressRev ) ar;

Monday, January 25, 2021 - 5:12:02 AM - Babita Back To Top (88096)
hello. i need a help. i tried the code and it is working well, but i need to insert the output into a another table. how to do this??if anyone help me. please help

Monday, October 5, 2020 - 5:16:14 PM - saminda Back To Top (86601)
wow this is really really great, I spent almost one day coding this thing by myself using charindex e.t.c
this is great, thank you, and wish you very best, you are a great DBA.

Thursday, October 1, 2020 - 1:02:10 PM - Adam Walker Back To Top (86580)
Really helpful. But I have a slightly more challenging item that I'm struggling with. I have a single columns that can hold an Email (prefixed by EM), a Mobile phone (prefixed by MO), Fax Phone No. (prefixed by FX), phone number 1 (prefixed by P1) and phone number 2 (prefixed by P2) all of which can appear in different orders and may contain trailing spaces. I've also noticed in some cases these fields are present on more than 1 occasion. Phone numbers are typically displayed as 32 characters and the total field length is a max of 224 characters. Any suggestions on reliable passing these as seperate columns as I need to perform data quality checks on these.

Tuesday, February 25, 2020 - 11:22:01 AM - Ben Back To Top (84790)

Carefull! There are many caveats to using this.  There is a limitation on length of string, how many delimiters you can have, your text cannot have any periods in it are some of the ones I have found.  Reverse is used because results are ordered/returned from right to left. Any errors result in a null return.


Monday, February 24, 2020 - 8:54:47 AM - Gerard Back To Top (84762)

Great tip! It would be good to mention, that PARSENAME function is designed for the specific purpose - to parse names of database object names - and it is limited to four parts in these names, limited by dot: "Returns the specified part of an object name. The parts of an object that can be retrieved are the object name, owner name, database name, and server name.". Without a mention like this the tip suggests, that PARSENAME function is a general purpose, which is not true.


Wednesday, February 19, 2020 - 3:21:15 PM - Aubrey Back To Top (84644)

Ozan:

Thank you for your reply. Yes, there a dozen different ways of parsing out the data of a string, this is just one of them. The REVERESE simply allows for a way of ordering the comma delimited string in your results. This sample was just a simple example that works across multiple versions of SQL Server Management Studio uniformly.


Wednesday, February 19, 2020 - 8:00:13 AM - Ozan Dikerler Back To Top (84635)

Actually, you don't need to REVERSE 2 times. I removed all reverse functions and it still works good. Why do you need reverse?















get free sql tips
agree to terms