简体   繁体   中英

Oracle — finding values with leading or trailing spaces

I am trying to find if a certain column requires TRIM function on it.

How can I find out if this column in a table has records that have white space either before or after the actual data.

You can check it using the TRIM function itself, not the most efficient but accurate:

Select *
From TableA
Where MyColumn <> TRIM(MyColumn)

Though if you're checking then turning around to trim anyway, you probably want to just do it in the first place, like this:

Select TRIM(MyColumn) as TrimmedMyColumn
From TableA

快速而肮脏的方式

WHERE LENGTH(TRIM(COL1)) <> LENGTH(COL1)

You could use regular expressions in Oracle.

Example:

select * from your_table 
 where regexp_like(your_column, '^[ ]+.*')
    or regexp_like(your_column, '.*[ ]+$')

So why can't you use the following to find the leading spaces? I've been able to identify the records with leading spaces this way and using '% ' to find the trailing spaces.

SELECT mycolumn
FROM my_table
WHERE mycolumn LIKE ' %'

I've also used the following to remove both the leading and trailing spaces

Update My_table set Mycolumn = TRIM(Mycolumn) 

which seems to work just fine.

select data1, length(data1)-length(replace(data1,' ','')) from t;

如果其中一个表字段T $ DSCA在末尾有尾随空格,则查询将检索行:SELECT * from TABLE_NAME A WHERE RAWTOHEX(SUBSTR(AT $ DSCA,LENGTH(T $ DSCA),1))='A0'和TRIM (T $ DSCA)不为空;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM