Oracle 10g 中的 REPLACE 没有按我的预期工作,有人能解释一下吗?

发布于 2024-10-16 23:38:12 字数 559 浏览 4 评论 0原文

我使用的是 Oracle 10g Express。

我有一个名为 arts_i_hate 的表,我已简化以更清楚地说明问题

ID | Name       | Opinion
-----------------------------
11 | jamesblunt | i hate him 

我运行语句

SELECT * FROM artists_i_hate WHERE to_char(ID)=REPLACE(to_char(1.1), '.');

为什么我得到“未找到数据”我在文档中的任何位置都找不到解释。

顺便说一句,我知道以下内容有效:

SELECT * FROM artists_i_hate WHERE to_char(ID)=REGEXP_REPLACE(to_char(1.1), '[^0-9]');

所以我认为另一个语句不起作用,因为它不喜欢替换某些符号。

编辑:

正在阅读前 2 个响应后对原始环境进行测试

I am using oracle 10g express.

I have a table named artists_i_hate, I have simplified to illustrate the problem clearer

ID | Name       | Opinion
-----------------------------
11 | jamesblunt | i hate him 

I run the statement

SELECT * FROM artists_i_hate WHERE to_char(ID)=REPLACE(to_char(1.1), '.');

Why do I get 'no data found' I can't find an explanation anywhere in the documentation.

btw I am aware that the following works:

SELECT * FROM artists_i_hate WHERE to_char(ID)=REGEXP_REPLACE(to_char(1.1), '[^0-9]');

So I am thinking the other statement doesn't work because it doesn't like replacing certain symbols.

edit:

Pending testing on original environment having read first 2 responses

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-10-23 23:38:12

它可能取决于 NLS 设置,因为在某些语言中 .不是小数点分隔符,因此 to_char(1.1) 不会给出“1.1”,

SQL> alter session set nls_numeric_characters = ',.';

Session altered.

SQL> select to_char(12.34) from dual;

TO_CH
-----
12,34

在这种情况下,REPLACE 不会更改任何内容,因此 ID 不匹配。

附言。
如果这是问题所在,一种解决方法是

select to_char(1.25,'999.99','NLS_NUMERIC_CHARACTERS=.,') FROM DUAL

It may depend on NLS settings as, in some languages the . is not the decimal separator so to_char(1.1) would NOT give '1.1'

SQL> alter session set nls_numeric_characters = ',.';

Session altered.

SQL> select to_char(12.34) from dual;

TO_CH
-----
12,34

In which case the REPLACE wouldn't change anything and therefore the ID wouldn't match.

PS.
If this is the issue, one fix would be

select to_char(1.25,'999.99','NLS_NUMERIC_CHARACTERS=.,') FROM DUAL
我要还你自由 2024-10-23 23:38:12

下面这表明这两个表达式之间完全没有区别。它们是完全等效的,因此如果 REGEXP_REPLACE 有效,那么 REPLACE 也会有效。

CREATE TABLE tester2 AS
SELECT
    REGEXP_REPLACE(to_char(1.1), '[^0-9]') Col1,
    REPLACE(to_char(1.1), '.') Col2
from dual;

select * from tester2;    
select * from USER_TAB_COLUMNS where table_name = 'TESTER2';

输出:

COL1    COL2
11    11

TABLE_NAME COLUMN_NAME DATA_TYPE  DATA_LENGTH   
TESTER2 COL1        VARCHAR2   2
TESTER2 COL2        VARCHAR2   2

This below shows that there is absolutely no difference between the two expressions. They are completely equivalent so if the REGEXP_REPLACE works, so will the REPLACE.

CREATE TABLE tester2 AS
SELECT
    REGEXP_REPLACE(to_char(1.1), '[^0-9]') Col1,
    REPLACE(to_char(1.1), '.') Col2
from dual;

select * from tester2;    
select * from USER_TAB_COLUMNS where table_name = 'TESTER2';

Output:

COL1    COL2
11    11

TABLE_NAME COLUMN_NAME DATA_TYPE  DATA_LENGTH   
TESTER2 COL1        VARCHAR2   2
TESTER2 COL2        VARCHAR2   2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文