×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

postgres sql - avg on a column defined as character

postgres sql - avg on a column defined as character

postgres sql - avg on a column defined as character

(OP)
If a column is defined as a character but it contains numeric values, can I use avg on such column or I have to do some type of a cast on the column.
Right now I get:
"unable to select an aggregate function avg(character varying"
on
 select avg(a) from table;
Thanks a lot
from "Just started to use PostGres SQL"

RE: postgres sql - avg on a column defined as character

hi babcia01,

You'll just have to convert it to a number so that you could use the avg function.

Assuming you have this table:

    Table "Test"
 Attribute |     Type     | Modifier
-----------+--------------+----------
 a         | character(5) |


If you try this statement:

select avg(to_number(a,99999)) from test;

It should give you the average of the rows in it.  The 99999 represents the type(9 for numbers i think) and length of your column (5 digits).

Hope this helps. :)

RE: postgres sql - avg on a column defined as character

(OP)
Thanks for the answer.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close