[pgsql-jp: 27978] Re: 文字列を数値でselect する方法

Mashiki mashiki @ yanah.com
2002年 11月 16日 (土) 00:01:40 JST


 Mashikiです。

> create table testtable
> {
>     id     int,
>     value  char(10)
> };
このSQLはinfomix特有なのでしょうか?括弧が不思議ですね。


create table testtable (
    id     int,
    value  char(10)
);
insert into testtable values(1,    '1234');
insert into testtable values(2, '0001234');
insert into testtable values(3, '   1234');


として、postgresql7.2.1で試しましたが、

 select id from testtable where value=1234;

だと1件もヒットしません。

select id from testtable where int8(trim(value))=1234;

や

select id from testtable
 where to_number(trim(value),'999999999')=1234;

ならすべて、検索できます。

select id from testtable
 where to_number(ltrim(rtrim(value)),'999999999')=1234;

なら、Oracleでも共通でいけるのではないでしょうか

# でも、これだとインデックスが効かないので、はじめから数値型で
# 定義しておきたいところ



pgsql-jp メーリングリストの案内