Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

i have a table in sql,

where in the columns while inserting it has taken white at left of every column.

so, i have tried ltrim(rtrim(newref)) but it is not removing the white space.

even, i have tried this as well,

ltrim(left(RTRIM(replace(New_Ref, char(160), char(32))),3)


but still its not triming...

Can anyone please help me.


thanks
Posted
Comments
DamithSL 26-Nov-14 11:28am    
can you update the question with your insert code?
are you trying to select data or update?
jaket-cp 26-Nov-14 11:31am    
maybe it is not a space
try something like select ascii(substring(ColumnNm, 1, 1)) from theTable; to see what character it is.
abdul subhan mohammed 26-Nov-14 11:35am    
this updated col value to 9 in each row... instead of col value
jaket-cp 26-Nov-14 12:08pm    
not sure what you mean.
Ah got you, yes Char(9) is a tab, so replace tab with empty string.
abdul subhan mohammed 26-Nov-14 11:31am    
actually i have uploaded data from excel, because of that i got white space.

From the comments it appears as though tabs have been inserted and not spaces.
To remove them you could do something like this:
SQL
--set up test data
declare @t table(id int identity(1,1),col varchar(50));
insert into @t 
select '	hello' col union all
select '	world' col union all
select 'hello world' col union all
select '	hello' col union all
select 'world' col union all
select '	hello world' col union all
select '	world hello' col

--remove tab from start of string
update @t 
set col = substring(col, 2, len(col))
where substring(col, 1, 1) = char(9) ;
 
Share this answer
 
Ascii values from 32 to 126 contains Numbers[0-9],Alphabet[A-Z/a-z] and acceptable special characters.

So you can check each character of your string to have this ascii value and if not then remove that character.

You can create a function of below code and use it to check each row of your table

SQL
Declare @Str varchar(50)='Hi you …there'
DECLARE @Result nvarchar(max)
    SET @Result = ''
 
    DECLARE @char nvarchar(1)
    DECLARE @charpos int
 
    SET @charpos = 1
    WHILE @charpos <= LEN(@Str)
    BEGIN
        SET @char = SUBSTRING(@Str , @charpos, 1)
 
        IF ASCII(@char) >=32 and ASCII(@char) <=126
            SET @Result = @Result + @char
        SET @charpos = @charpos + 1
    END
 
    select @Result
 
Share this answer
 
you might find this link useful sql-trim
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900