[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Find, Extract and Replace Data in a Field

Posted on 2006-05-25
Medium Priority
Last Modified: 2008-03-06
Okay, here's a stumper. I know there are you experts out there who know a lot more about T-SQL than I will ever know. But then, I was an electrician for 14 years before becoming a programmer, so at least I can wire up your house.

I am doing a system conversion. The current employee table has, like most, a first and last name. I am adding a column to the new table called CommonName. So if your name is William Smith, but you go by Bill, the FirstName column would contain William and the CommonName field would contain Bill.

Here is where it gets tricky. In the current system, if a person uses a common name, HR will enter 'William (Bill)' or 'William "Bill"'. Do you see where I am going with this yet?

I want to find either a " or a '(' in the field, extract the information from that point to the 'closing token - either a " or ')', strip out the 'tokens' and put the result in the CommonName field, and update the FirstName field with the data up to the 'token'. In other words, I would replace 'William (Bill)' with 'William' and put Bill in the CommonName field. I'll bet it can be done in T-SQL, I just don't know how.
Question by:dbbishop
  • 3
LVL 34

Expert Comment

by:Brian Crowe
ID: 16765365
SELECT RTRIM(SUBSTRING(@text, 1, CHARINDEX(@delim, @text) - 1)) AS FirstName,
      SUBSTRING(@text, CHARINDEX(@delim, @text) + 1, LEN(@text) - CHARINDEX(@delim, @text) - 1) AS CommonName
LVL 34

Expert Comment

by:Brian Crowe
ID: 16765380
oops...little premature on that one

DECLARE @delim char(1)
SET @delim = '"'

UPDATE myTable
SET FirstName = RTRIM(SUBSTRING(FirstName, 1, CHARINDEX(@delim, FirstName) - 1)),
   CommonName = SUBSTRING(FirstName, CHARINDEX(@delim, FirstName) + 1, LEN(FirstName) - CHARINDEX(@delim, FirstName) - 1)

run it twice with each delimiter '(' and '"'
LVL 34

Accepted Solution

Brian Crowe earned 1500 total points
ID: 16765386
that should have read...

run it once with each delimiter '(' and '"'
LVL 15

Author Comment

ID: 16765507

It works when there is a delimiter, but if there isn't, I get an error (Invalid length parameter passed to the substring function.)

I made the following change and it works.

UPDATE tblEmployees
SET FirstName = CASE WHEN CHARINDEX(@delim, FirstName) = 0 THEN FirstName
        ELSE RTRIM(SUBSTRING(FirstName, 1, CHARINDEX(@delim, FirstName) - 1)) END,
CASE WHEN CHARINDEX(@delim, FirstName) = 0 THEN ''
        ELSE SUBSTRING(FirstName, CHARINDEX(@delim, FirstName) + 1, LEN(FirstName) - CHARINDEX(@delim, FirstName) - 1) END

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question