如何删除sql中的空格 [英] how to remove white space in sql

查看:117
本文介绍了如何删除sql中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i有sql表,



插入时列中的位置它在每一列的左边都是白色的。



所以,我尝试过ltrim(rtrim(newref)),但它没有移除空格。



甚至,我也试过这个,



 ltrim(左(RTRIM(替换(New_Ref,char(160),char(32))),3)





但是它仍然没有转向...



任何人都可以帮助我。





谢谢

解决方案

从评论中看来,好像已插入标签而不是空格。

要删除它们,您可以执行以下操作:

   -   设置测试数据 
声明 @ t table (id int identity 1 1 ),col varchar 50 ));
插入 进入 @ t
选择 ' hello' col union all
选择 ' world' col union 全部
选择 ' hello world' col union all
select ' hello' col union all
选择 ' world' col union all
选择 ' hello world' col union all
选择 ' world hello' col

- 从字符串开头删除标签
update @ t
set col = substring(col, 2 ,len(col))
其中 substring(col, 1 1 )= char 9 ) ;


Ascii值从32到126包含数字[0-9],字母[AZ / az]和可接受的特殊字符。



所以你可以检查字符串的每个字符是否具有ascii值,如果没有则删除该字符。



你可以创建下面代码的函数并用它来检查表的每一行



 声明  @ Str   varchar  50 )= ' 你......  
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 ASCII( @ char )< = 126
SET @Result = @ Result + @ char
SET @ charpos = @ charpos + 1
END

选择 @ Result


您可能会发现此链接很有用sql-trim

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

解决方案

From the comments it appears as though tabs have been inserted and not spaces.
To remove them you could do something like this:

--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) ;


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

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


you might find this link useful sql-trim


这篇关于如何删除sql中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆