Modify field contents.  Remove "Atten" from a field that contains "Atten: First Name Last Name"

Posted on 2008-11-03
Medium Priority
Last Modified: 2012-05-05
Please help me with a SQL statement that will remove "Atten" from the Attent field that contains rows with:
Jane Doe
Atten Jane Doe
Attention Jan Doe
Jon Doe
Select * from Leas where Attent like 'Atten%'
will return the appropriate rows.  What addtional syntax will remove only the Atten and Attention and leave the names in the Attent field?
Question by:LFreehauf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 17

Expert Comment

ID: 22867884
select case
when left(attent,9)='Attention' then right(attent,len(attent)-9)
when left(attent,5)='Atten' then right(attent,len(attent)-5)
else attent
AS TrimmedAttentValue
from Leas

Author Comment

ID: 22868836
I think I need to clarify.
I want to delete any occurrence of "Atten" and "Attention" from the "Attent" field.
Your select statement returns data without the Atten or Attention (Thank You).  However, I want to modify data; remove ("Atten" and "Attention") from the field:
Example of 4 rows and the Attent field prior to modifications:
Jan Doe
Atten Jan Doe
Attention Jon Doe
Jon Doe

Example of the same 4 rows and the Attent field after the modification (desired results):
Jan Doe
Jan Doe
Jon Doe
Jon Doe
LVL 17

Accepted Solution

aflockhart earned 1500 total points
ID: 22874566
To modify the original data, use the UPDATE syntax below:  (I have also amended the code to delete the space between "Attention" or "Atten", and "Jane Doe")
Note that this will fail to find occurences where the word "attention" is spelled wrongly; and if there is any punctuation like "Attten : Jane Doe" it may give unwanted results

update leas 
set attent=
select case
when left(attent,9)='Attention' then ltrim(right(attent,len(attent)-9))
when left(attent,5)='Atten' then ltrim(right(attent,len(attent)-5))
else attent 
where left(attent,5)='Atten' 

Open in new window


Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

764 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