Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Trim leading and trailing tab spaces in MSSQL query

I have data which has leading and trailing spaces in the string. when storing that data in database I want to trim the space in query itself before storing into DB.

Normal spaces are trimming properly with RTRIM and LTRIM function but if a string contains tab space,its not trimming the tab space from the input string.

Can anyone help me to get the string with trimmed with tab space from leading and trailing.

like image 869
user3876801 Avatar asked Jan 13 '16 18:01

user3876801


1 Answers

Replace the ASCII code for tab (9):

replace(@str, char(9), '')

To only remove the outer tabs, first change them to something that won't exist in your data (I use a series of four spaces in this example), then rtrim/ltrim, then convert that same sequence back to tabs:

replace(ltrim(rtrim(replace(@str, char(9), '    '))),'    ', char(9));
like image 87
morgb Avatar answered Oct 28 '22 02:10

morgb