to_char in where子句SQL / Postgresql

时间:2012-09-16 16:10:09

标签: sql postgresql to-char

我正在使用Postgresql DB。

我想对表中的数字列执行查询,以查找该值是否以特定数字开头。

我目前有:

SELECT * FROM myTable WHERE to_char(ID, '12345678') LIKE '2%'

但是这会返回一个空数据集(但ID列中的值以2开头。

非常感谢

史蒂夫

5 个答案:

答案 0 :(得分:2)

这是因为TO_CHAR中数字的模式是9(具有指定位数的值)或0(带前导零的值)。使用以下内容:

SELECT * FROM myTable WHERE TRIM(to_char(ID, '99999999')) LIKE '2%'

或者将数字转换为字符。

答案 1 :(得分:2)

不要笑。作为强制转换的替代方法,您可以使用范围查询,例如:

SELECT * FROM mytable
WHERE id = 2
OR (id >=20 AND id < 30)
OR (id >=200 AND id < 300)
OR (id >=2000 AND id < 3000)
OR (id >=20000 AND id < 30000)
OR (id >=200000 AND id < 300000)
OR (id >=2000000 AND id < 3000000)
OR (id >=20000000 AND id < 30000000)
OR (id >=200000000 AND id < 300000000)
OR (id >=2000000000 AND id < 3000000000)
OR (id >=20000000000 AND id < 30000000000)
OR (id >=200000000000 AND id < 300000000000)
        -- ...
        ;

这可能导致postgres生成更好的查询计划,因为可以使用索引(如果存在,可以预期id字段)

更新: 查询计划:

Bitmap Heap Scan on reservations  (cost=1838.15..13290.11 rows=110809 width=4012) (actual time=11.310..24.379 rows=111111 loops=1)
   Recheck Cond: ((id = 2) OR ((id >= 20) AND (id < 30)) OR ((id >= 200) AND (id < 300)) OR ((id >= 2000) AND (id < 3000)) OR ((id >= 20000) AND (id < 30000)) OR ((id >= 200000) AND (id < 300000)) OR ((id >= 2000000) AND (id < 3000000)) OR ((id >= 20000000) AND (id < 30000000)) OR ((id >= 200000000) AND (id < 300000000)) OR ((id >= 2000000000) AND (id < 3000000000::bigint)) OR ((id >= 20000000000::bigint) AND (id < 30000000000::bigint)) OR ((id >= 200000000000::bigint) AND (id < 300000000000::bigint)))
   ->  BitmapOr  (cost=1838.15..1838.15 rows=112192 width=0) (actual time=11.242..11.242 rows=0 loops=1)
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.005..0.005 rows=1 loops=1)
               Index Cond: (id = 2)
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.58 rows=10 width=0) (actual time=0.003..0.003 rows=10 loops=1)
               Index Cond: ((id >= 20) AND (id < 30))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..2.52 rows=104 width=0) (actual time=0.013..0.013 rows=100 loops=1)
               Index Cond: ((id >= 200) AND (id < 300))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..14.24 rows=1036 width=0) (actual time=0.110..0.110 rows=1000 loops=1)
               Index Cond: ((id >= 2000) AND (id < 3000))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..144.73 rows=10845 width=0) (actual time=1.050..1.050 rows=10000 loops=1)
               Index Cond: ((id >= 20000) AND (id < 30000))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1332.24 rows=100196 width=0) (actual time=10.013..10.013 rows=100000 loops=1)
               Index Cond: ((id >= 200000) AND (id < 300000))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.010..0.010 rows=0 loops=1)
               Index Cond: ((id >= 2000000) AND (id < 3000000))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1)
               Index Cond: ((id >= 20000000) AND (id < 30000000))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1)
               Index Cond: ((id >= 200000000) AND (id < 300000000))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1)
               Index Cond: ((id >= 2000000000) AND (id < 3000000000::bigint))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.026..0.026 rows=0 loops=1)
               Index Cond: ((id >= 20000000000::bigint) AND (id < 30000000000::bigint))
         ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1)
               Index Cond: ((id >= 200000000000::bigint) AND (id < 300000000000::bigint))
 Total runtime: 28.720 ms
(28 rows)


 Seq Scan on reservations  (cost=0.00..19219.52 rows=4383 width=4012) (actual time=0.025..184.532 rows=111111 loops=1)
   Filter: ((id)::text ~~ '2%'::text)
 Total runtime: 189.100 ms
(3 rows)

答案 2 :(得分:2)

这将返回前导数字:

floor(a/10.^(floor(log(a))))

示例:

select a from generate_series(1,1000) as a where 
    floor(a/10.^(floor(log(a)))) = 2;

如果您的数字小于1或小于零,则应将其过滤掉(具体取决于您的要求)。

答案 3 :(得分:0)

我找到了答案:

SELECT * FROM myTable WHERE CAST (ID AS CHAR) LIKE '2%'

史蒂夫

答案 4 :(得分:-1)

您不需要to_char,只需id like '2%'