Solved

Remove last character from column conditionally

Posted on 2014-09-08
3
209 Views
Last Modified: 2014-09-08
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
Comment
Question by:Morpheus7
[X]
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
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40310059
UPDATE @Table
      SET Tag = case when  SUBSTRING(Tag,3,1) = 9 then LEFT(Tag, LEN(Tag) - 1)  else Tag end
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40310169
UPDATE @Table
       SET Tag = LEFT(Tag, LEN(Tag) - 1)
WHERE SUBSTRING(Tag,3,1) = '9'
0
 

Author Closing Comment

by:Morpheus7
ID: 40310177
Many thanks, that's great.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
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.

688 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