January 22, 2010 at 9:42 am
Hi,
I am trying to delete 3 characters from the right in a column. I can't find a function that will easily do that. The length of the string keeps changing. Below is some sample data. Please advise.
CA*461*NN
CA*461*NN
CA*461*NN
CA*463*NN
CA*463*NN
CCABM*100*01
CCART*100*01
CCART*100*01
CCICC*114*Z2
CCICC*Z2*Z2
CCLGA*110*01
CCLNG*100*01
CCLNG*100*01
CCLNG*102*01
CCSTD*103*01
CD*001*1S
CD*329*01
CD*329*31
CD*352*01
CD*352*01
CD*352*01
CD*352*01
CD*352*01
CD*352*01
CD*352*01
CD*352*01
CD*352*01
CS*762*51
CS*630*51
CS*772*61
SC*101D*01
SC*101D*02
SC*101*03
SC*101*04
SC*101D*05
------------
🙂
January 22, 2010 at 10:03 am
Try this if this works..
UPDATE Table SET Column = REPLACE( Column, RIGHT(Column, 3), '' )
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
January 22, 2010 at 10:06 am
Does this help get you in the right direction?
declare @ TestStr varchar(32); -- to get this to post, I added a space between @ and TestStr, remove that space
set @TestStr = 'SC*101D*05';
select @TestStr = left(@TestStr, len(@TestStr) - 3);
select @TestStr;
January 22, 2010 at 10:17 am
Lynn,
Thanks. It worked. Here was the solution.
SELECT left (column_name, len(column_name) -3) FROM Table_Name
------------
🙂
July 22, 2011 at 10:03 am
Just for the record Kingston's suggestion works fine n'all
July 22, 2011 at 10:10 am
Kingston Dhasian (1/22/2010)
Try this if this works..
UPDATE Table SET Column = REPLACE( Column, RIGHT(Column, 3), '' )
This won't work if the last 3 characters are used in the same sequence elsewhere in the string. I'd really go with the left(len)) option here.
July 22, 2011 at 11:45 am
Sugsy (7/22/2011)
Just for the record Kingston's suggestion works fine n'all
Actually, it doesn't. It works as long as the last three characters aren't repeated anywhere else in the string, but will not work if the characters are repeated. For example, if the original string is "CH*012*01", Kingston's solution will produce "CH2" whereas the correct solution is "CH*012'
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 22, 2011 at 4:18 pm
Sugsy (7/22/2011)
Just for the record Kingston's suggestion works fine n'all
Just for the record, it will also delete the wrong data if the right 3 occurs more than once in the string. Don't use it. 😉
(Sorry, Kingston)
--Jeff Moden
Change is inevitable... Change for the better is not.
July 22, 2011 at 4:19 pm
Gah... I've got to learn to read the rest of the posts first. Tow other folks basically said the same thing.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 22, 2011 at 4:57 pm
Jeff Moden (7/22/2011)
Gah... I've got to learn to read the rest of the posts first. Tow other folks basically said the same thing.
I would point out the obvious that there's a reason you're #1 non employee-poster. But that's been said already :hehe:.
July 23, 2011 at 12:21 pm
Ninja's_RGR'us (7/22/2011)
Jeff Moden (7/22/2011)
Gah... I've got to learn to read the rest of the posts first. Tow other folks basically said the same thing.I would point out the obvious that there's a reason you're #1 non employee-poster. But that's been said already :hehe:.
BWHAA-HAAA!!!! And the pot calls the kettle black. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 23, 2011 at 3:42 pm
Jeff Moden (7/23/2011)
Ninja's_RGR'us (7/22/2011)
Jeff Moden (7/22/2011)
Gah... I've got to learn to read the rest of the posts first. Tow other folks basically said the same thing.I would point out the obvious that there's a reason you're #1 non employee-poster. But that's been said already :hehe:.
BWHAA-HAAA!!!! And the pot calls the kettle black. 😉
Here you go, 3 free points. Tx for closing my case :-D.
July 23, 2011 at 5:12 pm
Ya know, Remi... I thought you were just joking.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 23, 2011 at 7:41 pm
Jeff Moden (7/23/2011)
Ya know, Remi... I thought you were just joking.
Of course I am. Why would you think otherwise ;-).
Viewing 15 posts - 1 through 15 (of 26 total)
You must be logged in to reply to this topic. Login to reply