Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the size of a varchar[n] field in one SQL statement?

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?

like image 440
Vivian River Avatar asked Apr 18 '11 15:04

Vivian River


People also ask

How do I find the size of a VARCHAR in SQL?

So remember to use LEN() function in SQL Server to find out the length of any String stored in VARCHAR column. It doesn't need to be VARCHAR, but LEN() function accepts a text value, which means it could be CHAR, VARCHAR, NCHAR or NVARCHAR as well.

How do I find the size of a field in SQL?

Use COL_LENGTH() to Get a Column's Length in SQL Server In SQL Server, you can use the COL_LENGTH() function to get the length of a column. More specifically, the function returns the defined length of the column, in bytes. The function accepts two arguments: the table name, and the column name.

What is length of VARCHAR in SQL?

The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

What is the default size of an in a column which is defined as VARCHAR N?

VARCHAR(n) In this syntax, n defines the string length that ranges from 1 to 8,000. If you don't specify n, its default value is 1.


2 Answers

select column_name, data_type, character_maximum_length       from information_schema.columns    where table_name = 'myTable' 
like image 159
Neil Knight Avatar answered Sep 28 '22 09:09

Neil Knight


On SQL Server specifically:

SELECT DATALENGTH(Remarks) AS FIELDSIZE FROM mytable 

Documentation

like image 43
MarcE Avatar answered Sep 28 '22 11:09

MarcE