Nov 8, 2013

How to know the length of column of a table in sql server?

We can get the column length of table by using the function COL_LENGTH

Syntax:

COL_LENGTH(Table_Name,Column_Name)

Parameters:

Table_Name: It is name of table. It is NVARCHAR(128) data type.

Column_Name: It the name of the column to which we want to find out the length of column.
Return type:

It returns integer value which is the length of data type of column. If either column name or table name doesn’t exist it returns NULL.

For example:
Suppose we have created a table by executing the following query:

CREATE TABLE Student(
    Roll_No BIGINT IDENTITY,
    Stu_Name VARCHAR(100),
    DOB DATETIME,
    IsActive BIT
)

We can get the size of each colmns of above table by executing the following sql query:

SELECT
    COL_LENGTH('Student','Roll_No'),
    COL_LENGTH('Student','Stu_Name'),
    COL_LENGTH('Student','DOB'),
    COL_LENGTH('Student','IsActive')

Output:
8   100 8   1

No comments:

Post a Comment