Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trimming Last 3 Numbers from some Records Expand / Collapse
Author
Message
Posted Monday, September 1, 2014 1:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 4:37 AM
Points: 113, Visits: 160
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
Post #1609253
Posted Monday, September 1, 2014 1:35 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 7:45 AM
Points: 1,125, Visits: 3,611
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
Post #1609263
Posted Monday, September 1, 2014 1:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 6,813, Visits: 14,028
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
Post #1609266
Posted Monday, September 1, 2014 2:40 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:21 AM
Points: 2,220, Visits: 6,004
Here is another method, find the last character which is not space or numeric


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
Post #1609278
Posted Monday, September 1, 2014 3:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 4:37 AM
Points: 113, Visits: 160
Hi ALL,

Thanks for your Replies . I will try the Methods which are necessary and will let you know

Post #1609292
Posted Monday, September 1, 2014 3:24 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 4:37 AM
Points: 113, Visits: 160
Eirikur Eiriksson (9/1/2014)
Here is another method, find the last character which is not space or numeric



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
Post #1609295
Posted Monday, September 1, 2014 3:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 6,813, Visits: 14,028
 ;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
Post #1609297
Posted Monday, September 1, 2014 3:42 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:21 AM
Points: 2,220, Visits: 6,004
Guitar_player (9/1/2014)
Eirikur Eiriksson (9/1/2014)
Here is another method, find the last character which is not space or numeric



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
Post #1609298
Posted Tuesday, September 2, 2014 6:37 AM This worked for the OP Answer marked as solution
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 6:34 AM
Points: 5, Visits: 2
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>))>0
THEN RTRIM(SUBSTRING(<Col_Name>,1,LEN(<Col_Name>)-(PATINDEX('%[0-9]%',REVERSE(<Col_Name>)))-2)) -- Please note, we have considered 3 numeric digits in this case
ELSE RTRIM(<Col_Name>) END AS New_String from <table>
Post #1609589
Posted Thursday, September 4, 2014 3:57 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 4:37 AM
Points: 113, Visits: 160
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>))>0
THEN RTRIM(SUBSTRING(<Col_Name>,1,LEN(<Col_Name>)-(PATINDEX('%[0-9]%',REVERSE(<Col_Name>)))-2)) -- Please note, we have considered 3 numeric digits in this case
ELSE RTRIM(<Col_Name>) END AS New_String from <table>


Hey .. thanks yaar . That did the Trick
Post #1610418
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse