Solved

Remove last character from column conditionally

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 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