Given this table:
and this query:
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION, IS_NULLABLE
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='PARAMETERS'
ORDER BY TABLE_NAME, ORDINAL_POSITION
I get this result:
How can I retrieve the size of the integer columns? For PAR_ID
(Lenght = 2) I get a NUMERIC_PRECISION
of 5.
Definition of NUMERIC_PRECISION
is "Precision of approximate numeric data, exact numeric data, integer data, or monetary data. Otherwise, NULL is returned". But why a value of 5 for smallint(2) and for example a value of 53 for float(8)? What does this values mean?
UPDATE:
Thanks to @Ed_Harper: there is difference between Length and Precision. Length is the number of bytes that are used to store the number. Precision is the number of digits in a number. See: http://goo.gl/EOTyC
The storage sizes of the members of the int family are fixed, as documented here (link is for SQL 2012 but this hasn't changed since SQL 2000):
tinyint = 1 byte
smallint = 2 bytes
int = 4 bytes
bigint = 8 bytes
The easiest way to get this information into a query will be to hard code it in a case statement.
NUMERIC_PRECISION
- determines the total number of decimal digits which can be stored in a numeric data type. This value is 5 for smallint
, 10 for int
etc. because that's the longest sequence of digits each datatype can store.
EDIT
Sample query showing use of case statement. Note that the list of datatypes is incomplete.
SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION, IS_NULLABLE,DATA_TYPE,
CASE DATA_TYPE WHEN 'tinyint' THEN 1
WHEN 'smallint' THEN 2
WHEN 'int' THEN 4
WHEN 'bigint' THEN 8
WHEN 'datetime' THEN 8
WHEN 'nvarchar' THEN CHARACTER_MAXIMUM_LENGTH * 2
WHEN 'nchar' THEN CHARACTER_MAXIMUM_LENGTH * 2
WHEN 'varchar' THEN CHARACTER_MAXIMUM_LENGTH
WHEN 'char' THEN CHARACTER_MAXIMUM_LENGTH
ELSE NULL
END AS column_size_bytes
FROM
INFORMATION_SCHEMA.COLUMNS
order by data_type