zeh*_*ron 5 sql postgresql casting
我在postgresql 9.1中有一个带有housenumber的示例表:
drop table if exists mytable;
create table mytable(road_id int, housenr text);
insert into mytable(road_id, housenr) values
('11', '1' ),
('22', '12' ),
('33', '99/1' ),
('44', '88' ),
('55', '2' ),
('66', '28' ),
('77', '29')
;
Run Code Online (Sandbox Code Playgroud)
现在我必须将整个列"housenr"转换为INT字段.SQL中是否有一种方法只能从可以转换的列中转换这些行.在mytable中,除了"housenr"= 99/1之外,这将是每一行.
类似于:FOR EACH ROW IF :: int IS POSSIBLE将行ELSE REMOVE FROM TABLE转换为
您可以使用REGEX评估列值以确定它是否为数字:
select * FROM MyTable where (housenr !~ '^[0-9]+$')
Run Code Online (Sandbox Code Playgroud)
这是SQLFiddle:
http://sqlfiddle.com/#!1/d2ff3/9
这是关于〜和〜的Postgresql文档!
http://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-TABLE
| 归档时间: |
|
| 查看次数: |
2000 次 |
| 最近记录: |