zoukankan      html  css  js  c++  java
  • 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 参数第一个字符的 ASCII ascii('x') 120
    substring(string [from integer] [for integer]) text 抽取子字串 substring('Thomas' from 2 for 3) trim

    版权所有,欢迎转载
  • 相关阅读:
    ASP脚本获取服务器全部参数列表说明
    HTML基础教程
    HTML5代码大全
    CSS 属性大全
    Web前端单词大全
    css常用代码大全
    曾国藩:诚敬静谨恒!
    鼠标经过显示菜单
    月入3000+项目
    右侧菜单显示隐藏
  • 原文地址:https://www.cnblogs.com/xiaotie/p/171088.html
Copyright © 2011-2022 走看看