In this post, I am providing one TSQL script to remove the trailing characters from Number in SQL Server.
Before a few days ago, I did a data migration from old CSV files to SQL Server table and found that one of our value has some trailing characters like, km, kg.
Actually, this trailing character required to remove from that number column, so I have prepared one TSQL script to perform this task.
First, Create sample table and data:
CREATE TABLE tbl_RemoveCharacter
INSERT INTO tbl_RemoveCharacter VALUES (1,'8.5 km')
INSERT INTO tbl_RemoveCharacter VALUES (2,'452.3 kg')
INSERT INTO tbl_RemoveCharacter VALUES (3,'808')
INSERT INTO tbl_RemoveCharacter VALUES (4,'102.5 km')
INSERT INTO tbl_RemoveCharacter VALUES (5,NULL)
Select your number with any trailing characters:
,LEFT(Value,DATALENGTH(Value)-(PATINDEX('%[0-9]%',REVERSE(Value))-1)) AS ValueWithoutTrailChar
Script to remove trailing characters from Number column:
SET Value = LEFT(Value,DATALENGTH(Value)-(PATINDEX('%[0-9]%',REVERSE(Value))-1))
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.