TSQL - 如何查找列是否有空格char(32)?

时间:2009-07-25 02:22:16

标签: sql sql-server tsql

TSQL - 如何查找列是否有空格char(32)

select *
from [sometable]
where CHARINDEX(' ', [somecolumn]) > 0

不起作用?有什么想法吗?

8 个答案:

答案 0 :(得分:10)

你必须rtrim CHAR列。

CHAR列在右侧填充空格,直到达到最大长度。

当存储短于最大长度的字符串时,RTRIM有助于避免误报。

select * from [table] where rtrim(col) like '% %'



create table dropme
(foo char(32))

insert into dropme values('nospaces')
insert into dropme values('i have a space')
insert into dropme values('space bar')

select replace(foo,' ','|') from dropme
where foo like '% %'

nospaces
i|have|a|space
space|bar

select replace(foo,' ','|') from dropme
where rtrim(foo) like '% %'

i|have|a|space
space|bar

答案 1 :(得分:5)

以下示例应说明如何实现此目标。

create table #tableTest
(
    someData varchar(100) not null
);

insert into #tableTest(someData) values('dsadsa');
insert into #tableTest(someData) values('fdssf 432423');
insert into #tableTest(someData) values('432423fsdv');
insert into #tableTest(someData) values('321 jhlhkj 543');



select * 
from #tableTest;

select * 
from #tableTest 
where charindex(char(32),someData) > 0;

drop table #tableTest;

答案 2 :(得分:2)

“不起作用”是什么意思?这两种方式都适合我:

SELECT ''''+a+'''' FROM(
SELECT 'asd fgh' AS a UNION ALL
SELECT ' fgh' AS a UNION ALL
SELECT 'asd ' AS a UNION ALL
SELECT 'asfdg') As t
WHERE a LIKE '% %'

---------
'asd fgh'
' fgh'
'asd '

SELECT ''''+a+'''' FROM(
SELECT 'asd fgh' AS a UNION ALL
SELECT ' fgh' AS a UNION ALL
SELECT 'asd ' AS a UNION ALL
SELECT 'asfdg') As t
WHERE CHARINDEX(' ', a) > 0

---------
'asd fgh'
' fgh'
'asd '

答案 3 :(得分:1)

以下对我有用:

val a: List[(Int, Int)] = dicP_SO.getOrElse(yi, List.empty[(Int, Int)] // return empty list if the key doesn't exists

答案 4 :(得分:0)

select * from [sometable] where somecolumn like '% %'

答案 5 :(得分:0)

鉴于你没有真正解释问题是什么......

您在寻找hardspace (nbsp)CHAR(160)吗?还是tab CHAR(9)? 这些看起来像空格但不是

答案 6 :(得分:0)

使用以下查询:

SELECT UniqueName 
FROM employee
WHERE employeeName LIKE '% %'

答案 7 :(得分:0)

我有和措词完全相同的问题,尽管我想看看该列是否有空格vs是否为空。这对我有用:

SELECT UniqueName 
FROM employee
WHERE ascii(employeeName) = 32

直观查询无效;下面还返回所有空行:

SELECT UniqueName 
FROM employee
WHERE employeeName = ' '