I've been facing this issue for many years now and it came back again this morning. Actually, here is presented a problem dealing with character field that consist of mysterious "hidden" blank spaces.
Also, this might cause some troubles in while copying data and bringing it over to some other applications like MS Excel.
Well, here is the whole investigation and the solution that I've been using:
DECLARE @testString varchar(255)
set @testString = 'MY STRING '
/*Ok, it seems easy, let's try to trim
this. Huh, it doesn't work, the same result here.*/
SELECT testStringTrim = RTRIM(@testString)
/*Let's try to get the size*/
SELECT LenOfTestString = LEN(@testString)
/*This supposed to give us string
together with blank space, but not for tab though*/
SELECT DataLengthOfString= DATALENGTH(@testString)
SELECT ASCIIOfTab = ASCII(' ')
SELECT CHAR(9)
/*I always use this like a final
solution*/
SET @testString = REPLACE(REPLACE(REPLACE(@testString, CHAR(9), ''), CHAR(10), ''), CHAR(13), '')
/*
CHAR(9)
- Tab
CHAR(10) - New Line
CHAR(13) - Carriage Return
*/
thanks .. works for me...
ReplyDeleteYour final solution works but while concatenating new line to old one it removes space between last word of first line & first word of other line.
ReplyDeleteExample: line1
line2 = line1line2
bookmarked your blog. you're doing a great job
ReplyDeleteFollowing removes multiple tabs with one space:
ReplyDeleteSELECT REPLACE(REPLACE(REPLACE('STRING WITH MULTIPLE TABS',CHAR(9),CHAR(7) + ' '), ' '+CHAR(7), ''), CHAR(7), '')
Thanks.
Great job
ReplyDeleteWorks for me.
Amazing, it totally worked!
ReplyDeleteThanks Mladen. Very handy code!
ReplyDelete