469,594 Members | 2,145 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,594 developers. It's quick & easy.

What is the best way for storing lengths in a database?


Forgive me this stupid question, but what is the best way to store the
length of an object in a database? What datatype would you use? Would
it be better to define one of your own?

Nov 12 '05 #1
2 1269
Probably an INTEGER or a BIGINT that is NULLable. For convenience, you
might use the LENGTH function to calculate the length of such objects,
and use a standard data type that can handle any possible value returned.

Example:

select length(col1), length(col2), length(col3), length(col4) from
testdec

1 2 3 4
----------- ----------- ----------- -----------
13 13 9 9

1 record(s) selected.

Details of the LENGTH function from the SQL Reference manual:

"The LENGTH function returns the length of a value.

The argument can be an expression that returns a value of any built-in
data type.

The result of the function is a large integer. If the argument can be
null, the result can be null; if the argument is null, the result is the
null value.

The result is the length of the argument. The length does not include
the null indicator byte of column arguments that allow null values. The
length of strings includes blanks but does not include the length
control field of varying-length strings. The length of a varying-length
string is the actual length, not the maximum length.

The length of a graphic string is the number of DBCS characters. The
length of all other values is the number of bytes used to represent the
value:

2 for small integer
4 for large integer
(p/2)+1 for decimal numbers with precision p
The length of the string for binary strings
The length of the string for character strings
4 for single-precision floating-point
8 for double-precision floating-point
4 for date
3 for time
10 for timestamp"

Pekka Henttonen wrote:
Forgive me this stupid question, but what is the best way to store the
length of an object in a database? What datatype would you use? Would
it be better to define one of your own?


Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Hickman | last post: by
39 posts views Thread by windandwaves | last post: by
5 posts views Thread by =?Utf-8?B?Q2hyaXM=?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.