1

Running postgres with postgis extension: trying to change datatype of column during a select into table statement

The column sum_popint in the munsummary table is double and I want to change it into an integer column during the select statement. I am aware I can alter the column datatype to integer before or after the select into statement using the update/alter statements but i want to do it within the select statement.

SELECT county,
       SUM(sum_popint) AS residentialpopulation,
       st_union(geom)
INTO countysummary
FROM munsummary
GROUP BY county;

2 Answers 2

5

what you are looking to do is CAST it to an integer. This takes the form of CAST ( expression AS type );

So in your SELECT, try:

SELECT county,
       CAST(SUM(sum_popint) as INTEGER) AS residentialpopulation,
       st_union(geom)
INTO countysummary
FROM munsummary
GROUP BY county;
Sign up to request clarification or add additional context in comments.

Comments

3

You could also use the shorthand syntax SUM(sum_popint)::int :

SELECT county,
   SUM(sum_popint)::int AS residentialpopulation,
   st_union(geom)
INTO countysummary
FROM munsummary
GROUP BY county

1 Comment

I contemplated using the shorthand, but figured that CAST might be more useful as it ansi sql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.