Solved

Remove last character from column conditionally

Posted on 2014-09-08
3
200 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:ScottPletcher
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now