SELECT COUNT() return typeof string
See original GitHub issueusing postgres 9.2 and pg moudle 2.0.0
script like following, the count type is number in 1.1.3, but string in 2.0.0, is it by design?
pgClient.query "SELECT COUNT(*) AS cnt FROM abc;", (err, results) ->
count = results.rows[0].cnt
console.log typeof(count)
pgClient.end()
Thanks Qiang
Issue Analytics
- State:
- Created 10 years ago
- Reactions:11
- Comments:13 (11 by maintainers)
Top Results From Across the Web
Postgres cast count(*) as integer? - node.js - Stack Overflow
I'm not seeing it: select pg_typeof(count(*)) from cell_per; bigint . · @S-Man is corect. · My guess is age has a declared type...
Read more >SQL Count(*) return type? | Ars OpenForum - Ars Technica
In Oracle, the fastest way to get a COUNT() is to use an indexed column, i.e. SELECT COUNT(id) FROM staff or suchlike.
Read more >Return type of [select count() from sObject where Condition ]
Hi guys I am trying to counting total rows in opportunity object using count() but its giving me an errors says Compile Error:...
Read more >Documentation: 9.5: Aggregate Functions - PostgreSQL
A query like: SELECT count(*) FROM sometable;. will require effort proportional to the size of the table: PostgreSQL will need to scan either...
Read more >What is the return type of a “count” query against MySQL using ...
What is the return type of a “count” query against MySQL using Java JDBC? ... useSSL=false", "root","123456"); String yourQuery = "SELECT ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Unfortunately those are going to return strings now since the return type of the
COUNT
operator isbigint
which, by definition, can exceed the maximum value of an int in JavaScript. I know it’s extremely unlikely you’d ever have more than one hundred billion rows or whatever the actualNumber.MAX_VALUE
is, but it’s the only way to be correct. We’ve gone back and forth and back and forth on number type conversions, and converting numbers to integers when they can possibly loose precision is the wrong way to do things.It really sucks it introduces such a large backwards compatibility issue, but that’s why I bumped the major version. I would suggest if you know you’re never going to have more than
Number.MAX_VALUE
data in your database you provide a custom type parser for the number types you’d like to receive as integers. I’ve created an “example” module for reference here:https://github.com/brianc/node-pg-parse-float
I think so, yeah. Sometimes I like to leave these kinda issues open as informative. But…github search is a lot better than it used to be so that’s probably a waste now. 😄