SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trimming Last 3 Numbers from some Records


Trimming Last 3 Numbers from some Records

Author
Message
Guitar_player
Guitar_player
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 236
Hi ,

I have column which are having data as Alphabets and also Numbers like to say

ID Column1
1 ABCD 123
2 XY 567
3 ghj 001
4 RUT
5 PUT


like that i have many records now i want to Trim the Last digits with numbers and keep only the string names like

ID Column1
1 ABCD
2 XY
3 ghj
4 RUT
5 PUT

Can you guys help me out please :-)
Neil Burton
Neil Burton
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3469 Visits: 10189
Will the records always have a space in them and will it always be three numbers?


On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
—Charles Babbage, Passages from the Life of a Philosopher

How to post a question to get the most help
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16794 Visits: 19557
Guitar_player (9/1/2014)
Hi ,

I have column which are having data as Alphabets and also Numbers like to say

ID Column1
1 ABCD 123
2 XY 567
3 ghj 001
4 RUT
5 PUT


like that i have many records now i want to Trim the Last digits with numbers and keep only the string names like

ID Column1
1 ABCD
2 XY
3 ghj
4 RUT
5 PUT

Can you guys help me out please :-)


REVERSE()
Then check the first four characters - if they are three digits followed by a space, use STUFF to remove the first four characters, otherwise leave alone
REVERSE()

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15586 Visits: 18623
Here is another method, find the last character which is not space or numeric
Cool

USE tempdb;
GO

;WITH SAMPLE_DATA(ID,Column1) AS
(SELECT ID,Column1 FROM
(VALUES
(1,'ABCD 123')
,(2,'XY 567')
,(3,'ghj 001')
,(4,'RUT')
,(5,'PUT')) AS X(ID,Column1)
)
,T(N) AS (SELECT N FROM ( VALUES (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL)) AS X(N))
SELECT
SD.ID
,SUBSTRING(SD.Column1,1,MAX(NM.N)) AS CLEAN_COL
FROM SAMPLE_DATA SD
CROSS APPLY
(SELECT TOP(LEN(SD.Column1)) (LEN(SD.Column1) + 1) - ROW_NUMBER()
OVER (ORDER BY (SELECT NULL)) AS N FROM T T1,T T2,T T3,T T4,T T5,T T6,T T7) AS NM(N)
WHERE ASCII(SUBSTRING(SD.Column1,NM.N,1)) NOT IN (32,48,49,50,51,52,53,54,55,56,57)
GROUP BY SD.ID,SD.Column1;



Results
ID          CLEAN_COL
----------- ---------
1 ABCD
2 XY
3 ghj
4 RUT
5 PUT

Guitar_player
Guitar_player
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 236
Hi ALL,

Thanks for your Replies . I will try the Methods which are necessary and will let you know
Guitar_player
Guitar_player
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 236
Eirikur Eiriksson (9/1/2014)
Here is another method, find the last character which is not space or numeric
Cool


Hi Eiriksson,

There are also quite records like 'ABC DEF 210 ' , 'ggh / sah ggh 590'...
So does the logic applies for these too ?

If possible can i get any function directly for these ..to check if the last digits are numbers ..if they are numbers then delete them and also the sapce and if not Trim the last space

Thanks & Regards,
Guitar Player
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16794 Visits: 19557
 ;WITH SampleData AS (
SELECT MyString = 'ABC DEF 210 ' UNION ALL
SELECT 'ggh / sah ggh 590' UNION ALL
SELECT 'ggh / sah gghXPT' UNION ALL
SELECT 'ggh / sah ggh123')

SELECT *,
NewString = CASE
WHEN SUBSTRING(rMyString,4,1) = ' ' AND LEFT(rMyString,3) NOT LIKE '%[^0-9]%' THEN REVERSE(STUFF(rMyString,1,4,''))
ELSE MyString END
FROM SampleData
CROSS APPLY (SELECT rMyString = REVERSE(RTRIM(MyString))) d



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15586 Visits: 18623
Guitar_player (9/1/2014)
Eirikur Eiriksson (9/1/2014)
Here is another method, find the last character which is not space or numeric
Cool


Hi Eiriksson,

There are also quite records like 'ABC DEF 210 ' , 'ggh / sah ggh 590'...
So does the logic applies for these too ?

If possible can i get any function directly for these ..to check if the last digits are numbers ..if they are numbers then delete them and also the sapce and if not Trim the last space

Thanks & Regards,
Guitar Player


That should be no problem, the code trims the string to the last Alpha character in the string

This set
 (1,'ABCD 123')
,(2,'XY 567')
,(3,'ghj 001')
,(4,'RUT')
,(5,'PUT')
,(6,'ABC DEF 210')
,(7,'ggh / sah ggh 590')
,(7,'ABC 123 DEF 456')


becomes this
ID          CLEAN_COL
----------- -----------------
1 ABCD
2 XY
3 ghj
4 RUT
5 PUT
6 ABC DEF
7 ggh / sah ggh
7 ABC 123 DEF

sateeshcvtly
sateeshcvtly
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
Hi,

I think below one will be the easiest way to query your data

SELECT <Col_Name>,CASE WHEN PATINDEX('%[0-9]%',REVERSE(<Col_Name>Wink)>0
THEN RTRIM(SUBSTRING(<Col_Name>,1,LEN(<Col_Name>Wink-(PATINDEX('%[0-9]%',REVERSE(<Col_Name>Wink))-2)) -- Please note, we have considered 3 numeric digits in this case
ELSE RTRIM(<Col_Name>Wink END AS New_String from <table>
Guitar_player
Guitar_player
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 236
sateeshcvtly (9/2/2014)
Hi,

I think below one will be the easiest way to query your data

SELECT <Col_Name>,CASE WHEN PATINDEX('%[0-9]%',REVERSE(<Col_Name>Wink)>0
THEN RTRIM(SUBSTRING(<Col_Name>,1,LEN(<Col_Name>Wink-(PATINDEX('%[0-9]%',REVERSE(<Col_Name>Wink))-2)) -- Please note, we have considered 3 numeric digits in this case
ELSE RTRIM(<Col_Name>Wink END AS New_String from <table>


Hey .. thanks yaar . That did the Trick :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search