指定SQL case语句的返回类型

时间:2013-05-13 17:44:04

标签: sql oracle oracle11g

我有以下SQL案例陈述。

select tracking_num,
case
    when source_one is not null then source_one
    else source_two
end source_value
from ...

source_one和source_two是双精度。如果我执行上面的SQL I,返回的source_value列是int precision。

同事建议在列名的开头添加ohd_。

select tracking_num,
case
    when source_one is not null then source_one
    else source_two
end ohd_source_value
from ...

返回正确的值(返回的列名为'source_value')。

ohd_前缀是oracle标准吗?我无法在任何地方找到文档。此外,还有更好的方法来执行此转换吗?也许通过使用CAST关键字?

感谢您的回复。

数据库版本:Oracle数据库11g企业版11.2.0.3.0版 - 64位

编辑:作为我提到的ohd_前缀的后续内容。事实证明,这是作为一个解决oracle bug#1312311的问题烘焙到我们的客户端。 OHD显然代表“Oracle Hack Double”。

2 个答案:

答案 0 :(得分:1)

你的情况似乎不对。

Oracle本身没有一些编码干预,只根据列别名转换类型。

但是,要完全确定您的退回类型,您可以在SELECT声明中CAST选定的值。

答案 1 :(得分:0)

请发一个简单的例子来说明你说的话。除非我误解你的情况,否则我看不到任何转变。

SQL> drop table testx
Table dropped.
SQL> create table testx
(
 pk number,
 d1 BINARY_DOUBLE
)
Table created.
SQL> drop table testy
Table dropped.
SQL> create table testy
(
  pk number,
  d1 BINARY_DOUBLE
)
Table created.
SQL> insert into testx values (1, 100)
1 row created.
SQL> insert into testx values (2, null)
1 row created.
SQL> insert into testy values (1, 200)
1 row created.
SQL> insert into testy values (2, 201)
1 row created.
SQL> commit
Commit complete.
SQL> drop table testz
Table dropped.
SQL> create table testz as
select x.pk,
case when (x.d1 is not null) then x.d1
else y.d1
end new_d1
from testx x, testy y
where x.pk = y.pk
Table created.
SQL> select column_name || ' => ' ||  data_type
from user_tab_columns
where table_name = 'TESTZ'

COLUMN_NAME||'=>'||DATA_TYPE                                                    
--------------------------------------------------------------------------------
PK => NUMBER                                                                    
NEW_D1 => BINARY_DOUBLE                                                         

2 rows selected.

另请注意,我通常不使用BINARY_DOUBLE(我只使用NUMBER类型),但您明确声明“source_one和source_two是双精度”,所以我推断了这一点。

相关问题