简体   繁体   中英

Is is possible to retrieve the maximum declared size of a varchar(n) column?

Suppose you have a DB table like this:

Table t
....
column_a     integer
column_b     varchar(255)
....

Now, I want to store a string that is composed by a list of names on t.column_b, with the following format (separated by commas):

Word A, Word B, Word C...

The problem is, it might be the case that the string is larger than 255 characters and in my application logic I don't want to blindly trim to 255, but instead store the maximum number of words possible, eliminating the last word that exceeds the size. Also, I want to develop in such a way that if the column changes size, I don't want to change my application. Is it possible to write a SQL query that retrieves the declared size of a column? Or perhaps, I should use another column type?

If relevant, I am using Informix.

Thanks in advance.

  1. Informix truncates blindly at the limit unless your database is MODE ANSI.
  2. The DBI defines metadata attributes for columns and DBD::Informix implements them.

    For a statement handle, $sth , you can use:

     $sth->{PRECISION}->[0] 

    to get the precision (length) of the first column in the output.

See perldoc DBI under 'Statement Handle Attributes'.

If you need to know the type information for some column, write a SELECT statement, prepare it, then analyze the statement handle.

Because this is defined by DBI, you will get the same behaviour with any driver (DBD::YourDBMS).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM