Statement for converting varchar to int4 in PostgreSQL

Source: Internet
Author: User

Today is depressing.

In the SHP file of National Basic Geographic Data, the provincial data adcodd99 is int type. I imported it into PostgreSQL and it became int4 type.

However, the adcodd99 of the district-level data is portable and imported into the database as varchar. After querying the PostgreSQL documentation, no function can be directly converted into a number of strings. Some research found that it can be converted using a combination of a bunch of string functions provided by it. The data table is geo_country_poly. I have created a new int4 adcodd99_int column. Run the following SQL statement to convert the data:

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)

Note:

function return type description example result
ASCII ( text ) integer ASCII code of the first character of the parameter ASCII ('x') 120
substring (string [from integer] [for integer]) text extract substring substring ('Thomas 'from 2 for 3) trim
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.