PostgreSQL中Varchar转化为int4的语句

    今天很郁闷。

    国家基础地理数据的shp文件中,省级数据的adcodd99为int型的,我导入到PostgreSQL中,成为int4类型。

    但是区县级数据的adcodd99为字符型的,导入到数据库中,成了varchar。查询了PostgreSQL文档,没发现直接将字符串转化为数的函数。一番研究发现,可以用它提供的一堆字符串函数的组合来进行转化。数据表为geo_country_poly。我在其中新建了一列int4型的adcodd99_int。运行以下sql语句就可以进行转化:

update geo_country_poly set adcodd99_int=
100000*(ascii(substring(adcode99 from 1 for 1))-48) +
10000*(ascii(substring(adcode99 from 2 for 1))-48) +
1000*(ascii(substring(adcode99 from 3 for 1))-48) +
100*(ascii(substring(adcode99 from 4 for 1))-48) +
10*(ascii(substring(adcode99 from 5 for 1))-48) +
1*(ascii(substring(adcode99 from 6 for 1))-48)

注:

函数返回类型描述例子结果
ascii(text)integer参数第一个字符的 ASCIIascii('x')120
substring(string [from integer] [for integer])text抽取子字串 substring('Thomas' from 2 for 3)trim

转载于:https://www.cnblogs.com/xiaotie/archive/2005/06/09/171088.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值