Solved

Remove last character from column conditionally

Posted on 2014-09-08
3
206 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
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

Industry Leaders: 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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 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