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?
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.
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.
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.
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.
select column_name, data_type, character_maximum_length from information_schema.columns where table_name = 'myTable'
On SQL Server specifically:
SELECT DATALENGTH(Remarks) AS FIELDSIZE FROM mytable
Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With