#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    1
    Rep Power
    0

    PSQL Field Length Query


    Is there a way to query the db for the maximum length of a field for error-checking/truncation before writing?

    Thanks.
  2. #2
  3. Me likey breadsticks...
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2003
    Location
    Los Angeles
    Posts
    1,189
    Rep Power
    18
    If you just wanna check beforehand (a 1 time thing) then you can do a \d table on your table in psql and get the info you want.

    If you want to do this interactively you can do the following (modified from the phppgadmin source) :
    PHP Code:
     SELECT
                    a
    .attnum,
                    
    a.attname AS field,
                    
    t.typname AS type,
                    
    a.attlen AS length,
                    
    a.atttypmod AS lengthvar,
                    
    a.attnotnull AS notnull
            FROM
                    pg_class c
    ,
                    
    pg_attribute a,
                    
    pg_type t
            WHERE
                    c
    .relname '$your_table'
                    
    and a.attnum 0
                    
    and a.attrelid c.oid
                    
    and a.atttypid t.oid
                    
    and a.attname '$your_field'
            
    ORDER BY a.attnum 
    just subsititute $your_table and $your_field.

    HTH,
    -b
    PostgreSQL, it's what's for dinner...

IMN logo majestic logo threadwatch logo seochat tools logo