简体   繁体   English

Oracle - 使用前导或尾随空格查找值

[英]Oracle — finding values with leading or trailing spaces

I am trying to find if a certain column requires TRIM function on it. 我试图找出某个列是否需要TRIM功能。

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: 您可以使用TRIM功能检查它,而不是最有效但准确的:

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. 您可以在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)不为空;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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