October 21, 2013 at 3:31 pm
have this problem where I've got a realname field (varchar (255)) where the front end doesn't enforce naming practices and allows leading zeroes. So sometimes the same name will end up in the database, in the same column, with leading zeros and sometimes not. I need to remove leading zeros from all names so that I can group by the realname.
Seems pretty simple to use LTRIM(RTRIM(expression)), but surprised to find the leading zeroes remain. I know they've remained because when I use the LEN() function against one of the offending names to show me how many characters long it is longer then that of the same name in another row. Plus, when I filter for name without the % wildcard, only the one returns.
Thought this would be simple ltrim/rtrim solution. Anyone have understanding of why I'm having this problem?
--Quote me
October 21, 2013 at 3:39 pm
LTRIM and RTRIM only remove leading and trailing white space. It doesn't remove zeros. If this is a name field are zeros allowed at all? If not, then try REPLACE() and replace all 0's with an empty space ''
October 21, 2013 at 3:43 pm
ach, sorry, I meant leading white spaces. NOT zeroes.
like this:
/****** Script for SelectTopNRows command from SSMS ******/
DECLARE @REALNAME varchar(255)
SET @REALNAME = ' Ricki Ricardo'
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
in this example the ltrim/rtrim works as expected. But for my real life scenario, the leading white spaces aren't getting removed. It's as though the blanks are something other than leading white spaces....but what could they be?
--Quote me
October 21, 2013 at 3:56 pm
You may have leading and/or trailing tabs instead of spaces which the trim's won't replace. You can use CHARINDEX to find CHAR(9) in a string and then use the replace function if that is the issue.
/****** Script for SelectTopNRows command from SSMS ******/
DECLARE @REALNAME varchar(255)
SET @REALNAME = ' Ricki Ricardo'
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
SET @REALNAME = 'Ricki Ricardo'
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
October 21, 2013 at 4:13 pm
I see what you're saying. I tried it with the below example, and it returned 1 for placevalue of first tab.
DECLARE @REALNAME varchar(255)
SET @REALNAME = 'Ricki Ricardo'--where the leading space is a single tab space
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
select charindex(char(9),@realname, 0)
But, in my case it's not a tab. The position value returned was 0. What other character could the white space be?
--Quote me
October 21, 2013 at 4:33 pm
I tried to see if converting to binary would expose a character type, but the beginning of binary out looks same in both cases.
DECLARE @REALNAME varchar(255)
SET @REALNAME = 'Ricki Ricardo'---leading space is a tab
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
select charindex(char(9),@realname, 0)
select convert(binary(15), @realname)--yields 0x095269636B69205269636172646F00
SET @REALNAME = ' Ricki Ricardo'
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
select charindex(char(9),@realname, 0)
select convert(binary(15), @realname)--yields 0x205269636B69205269636172646F00
--Quote me
October 21, 2013 at 4:38 pm
Keith Tate (10/21/2013)
You may have leading and/or trailing tabs instead of spaces which the trim's won't replace. You can use CHARINDEX to find CHAR(9) in a string and then use the replace function if that is the issue./****** Script for SelectTopNRows command from SSMS ******/
DECLARE @REALNAME varchar(255)
SET @REALNAME = ' Ricki Ricardo'
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
SET @REALNAME = 'Ricki Ricardo'
select LEN(@realname)
select len(RTRIM(ltrim(@realname)))
CHAR(160)... the notorious and dreaded "Hard Space".
--Jeff Moden
Change is inevitable... Change for the better is not.
October 21, 2013 at 4:41 pm
cha cha cha! You see I wouldn't have lasted to 160 if I'd started with char(0), char(1), char(2). I would have given up long before 160 thinking it was idleness.
Thanks so much.
---EDIT. That only took care of one of the white spaces! Any other common offenders?
--Quote me
October 21, 2013 at 4:51 pm
ok it was char(13). OK, thanks so much to both of you. Two replaces resolves the leading white spaces.
--Quote me
October 22, 2013 at 3:26 pm
polkadot (10/21/2013)
ok it was char(13). OK, thanks so much to both of you. Two replaces resolves the leading white spaces.
Ugh! 🙂 CHAR(13) is a carriage return. Sounds like they may have tried to force EOL returns instead of letting nature take it's course.
Thanks for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply