SQL Server : find rows that contains spaces

x9ybnkn6  于 2023-10-15  发布在  SQL Server
关注(0)|答案(4)|浏览(129)

I'm looking for fast way to see all rows in table that contains any spaces.

For starters, I tried to see which rows starts with space by this query:

select * 
from MyTable 
where ColumnName like ' %'

but I got 0 results, although I can see there are rows with spaces.

cmssoen2

cmssoen21#

Give this a try:

In SQL server you can use this:

SELECT * FROM MYTABLE 
WHERE CHARINDEX(' ',ColumnName) > 0;

If you are using Oracle you can use this:

SELECT * FROM MYTABLE 
WHERE INSTR(ColumnName,' ') > 0;

Essentially in this query it finds the character position containing first space from the column values and when it finds the first space in it the index value should be greater than 1 and it should display all the records based on that.

wwwo4jvm

wwwo4jvm2#

Seem that those space contain some other special character apart from char(10).

Try this,

select * 
from MyTable 
where patindex('%[^A-Z0-9]%',ColumnName )>0

First decide what is valid value for ColumnName .Then try PATINDEX and change the regex accordingly.

vuktfyat

vuktfyat3#

try put another '%', this may solve your problem.

select * from MyTable where ColumnName like '% %'

gab6jxml

gab6jxml4#

select * from TABLE where trim(column_name) IS NULL

相关问题