99

Suppose that I have a SQL table that has a varchar[1000] field called "Remarks".

I would like to craft a single SQL statement, which when executed, will return 1000, or whatever the size of the varchar field might be changed to in the future.

Something like SELECT size(Remarks) FROM mytable.

How do I do this?

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
Vivian River
  • 31,198
  • 62
  • 198
  • 313
  • This will depend on the RDBMS you are working on. But as Neil said you may have to search in the information scheme of your server. – Marc Bouvier Apr 18 '11 at 15:39

10 Answers10

152
select column_name, data_type, character_maximum_length    
  from information_schema.columns  
 where table_name = 'myTable'
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
33

On SQL Server specifically:

SELECT DATALENGTH(Remarks) AS FIELDSIZE FROM mytable

Documentation

UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
MarcE
  • 3,586
  • 1
  • 23
  • 27
  • 4
    This statement returns the length of the data that has already been input. I want the size of the data that the field will hold. – Vivian River Apr 18 '11 at 15:41
  • 1
    If you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s Apr 18 '11 at 15:49
18

For SQL Server (2008 and above):

SELECT COLUMNPROPERTY(OBJECT_ID('mytable'), 'Remarks', 'PRECISION');

COLUMNPROPERTY returns information for a column or parameter (id, column/parameter, property). The PRECISION property returns the length of the data type of the column or parameter.

COLUMNPROPERTY documentation

Frank
  • 658
  • 1
  • 8
  • 12
10

This will work on SQL SERVER...

SELECT COL_LENGTH('Table', 'Column')
Greg Quinn
  • 1,927
  • 1
  • 23
  • 26
  • 4
    Worth noting that this will return the length in *bytes* so you'll have to halve it for `NVARCHAR` or `NCHAR` for example. See https://learn.microsoft.com/en-us/sql/t-sql/functions/col-length-transact-sql – Bernhard Hofmann Jun 21 '17 at 08:56
1

I was looking for the TOTAL size of the column and hit this article, my solution is based off of MarcE's.

SELECT sum(DATALENGTH(your_field)) AS FIELDSIZE FROM your_table
ProVega
  • 5,864
  • 2
  • 36
  • 34
1

This is a function for calculating max valid length for varchar(Nn):

CREATE FUNCTION [dbo].[GetMaxVarcharColumnLength] (@TableSchema NVARCHAR(MAX), @TableName NVARCHAR(MAX), @ColumnName VARCHAR(MAX))
RETURNS INT
AS
BEGIN
    RETURN (SELECT character_maximum_length FROM information_schema.columns  
            WHERE table_schema = @TableSchema AND table_name = @TableName AND column_name = @ColumnName);
END

Usage:

IF LEN(@Name) > [dbo].[GetMaxVarcharColumnLength]('person', 'FamilyStateName', 'Name') 
            RETURN [dbo].[err_Internal_StringForVarcharTooLong]();
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
1
select column_name, data_type, character_maximum_length    
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'Table1'
Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
0

For t-SQL I use the following query for varchar columns (shows the collation and is_null properties):

SELECT
    s.name
    , o.name as table_name
    , c.name as column_name
    , t.name as type
    , c.max_length
    , c.collation_name
    , c.is_nullable
FROM
    sys.columns c
    INNER JOIN sys.objects o ON (o.object_id = c.object_id)
    INNER JOIN sys.schemas s ON (s.schema_id = o.schema_id)
    INNER JOIN sys.types t ON (t.user_type_id = c.user_type_id)
WHERE
    s.name = 'dbo'
    AND t.name IN ('varchar') -- , 'char', 'nvarchar', 'nchar')
ORDER BY
    o.name, c.name
Andrei Sura
  • 2,465
  • 1
  • 20
  • 15
  • Please be aware that this will display the max_length in bytes, meaning you'll get 'double' sizes for NVARCHAR columns. – Jens Aug 29 '19 at 09:21
0

For MS SQL Server this will return column length:

SELECT COL_LENGTH('dbo.mytable', 'Remarks') AS Result;
Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
0

For SQL Server, instead of SIZE we can use LEN rest everything given in question is fine. LEN returns number of characters, not sure I should call it eye friendly or brain friendly :)

SELECT LEN(Remarks) FROM mytable

DATALENGTH also works fine as given in other answers but it returns number of bytes.

SELECT DATALENGTH(Remarks) FROM mytable

Here is documentation reference

Vipin
  • 4,851
  • 3
  • 35
  • 65