Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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

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
etc
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?
Thanks
0
LFreehauf
Asked:
LFreehauf
  • 2
1 Solution
 
aflockhartCommented:
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
end
AS TrimmedAttentValue
from Leas
0
 
LFreehaufAuthor Commented:
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
0
 
aflockhartCommented:
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 
end 
where left(attent,5)='Atten' 

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now