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: 220
  • Last Modified:

Remove last character from column conditionally

Hi,

I would like to be able to update a table variable in that if the third character is a 9 then I need to remove the last character.

This is what I am using currently:
UPDATE @Table
      SET Tag = (SELECT LEFT(Tag, LEN(Tag) - 1) WHERE SUBSTRING(Tag,3,1) = 9)

This has the correct effect on the rows where the character is 9, but the other rows where the third character is not 9 are updated to NULL.

Any help would be appreciated.
Thanks
0
Morpheus7
Asked:
Morpheus7
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
UPDATE @Table
      SET Tag = case when  SUBSTRING(Tag,3,1) = 9 then LEFT(Tag, LEN(Tag) - 1)  else Tag end
0
 
Scott PletcherSenior DBACommented:
UPDATE @Table
       SET Tag = LEFT(Tag, LEN(Tag) - 1)
WHERE SUBSTRING(Tag,3,1) = '9'
0
 
Morpheus7Author Commented:
Many thanks, that's great.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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