To measure the length of VARCHAR fields the function LEN(varcharfield) is useful.
To measure the length of TEXT fields the function is DATALENGTH(textfield). Len will not work for text field.
Example:
SELECT DATALENGTH(yourtextfield) AS TEXTFieldSize
Reference : Pinal Dave (http://blog.SQLAuthority.com)




Really useful
Just what I wondered about.
Hi, It is really useful.
I have the database – in which i have to generate script for 10 tables only – as i dont have Enterprise Manager, i want to generate the script through Query Analyser onl.
ie the script should like the result of –Enterprise Manager—> Select the Table(s)–Rt Click–All Tasks — Generate SQL Script.
How can i get this?
Thanks in Advance.
Regards — Poov
Thnx!!
This was just what I needed
xx
Thank you! Before this I found a bunch of other SQL’s _languages_ that accept just LENGTH, but THIS is what I really needed.
Thank you!
This is what I was looking for.
Thanks
Nice …
but now how would i do that in Hibernate …
: )
Thanks for the info! Just what I was looking for…
Thanks :) You saved me lots of trouble…
I need some statement to check database field size in the code before inserting any value
@Antonio,
You can use this stord procedure
SP_HELP table_name
it will list out all the fields, their sizes, constraints, indexes….
It gives very good information.
Hope this helps.
Thanks,
Imran.
thanks , very useful
Thanks! You are the SQL man!
Vey helpful dude!! Keep up the good work!
Jai Hind!
Thank you sir! Just what I was looking for!
Thanks. Short and usefull info.
Really useful…
i want to store more than 8000 chars in table field..
i hv used varchar(max) , text(max) ,but i m not able to get success in it..
everytime data gets truncated whn i insert it..
so can u tell me how to overcome from this problem..?
I am having the same issue as Ramani. Any solution for this ?
I have a text field and what ever number of characters that I save on that field, it returns only 1001 characters. But when I checked with DATALENGTH() function it shows that it is having the same number of characters that I entered.
@Chaminda,
Open SQL Server Management Studio
Go to Tools – Options (Tools exists at the top left of SSMS)
Expand Query Results
Expand SQL Server
Click on Results to Grid
On right side, Set Value for :
Maximum Characters retrieved : Set a higher value.
Because of this, SQL Server is returning fewer characters. Change this value to a higher value and see the difference
~ IM.
Hello!
But, when the field is null ?
how can i know the size of a field, have or not have data in the table ??
Thanks!
Good work !!
Good tips!!!
Thanks bro
can anybody help me in using length function
how i can make any field have 4 digits (e.g 7456)
to 6 digits(i.e 007456) in my data base using sql query so that every field with length 4 is changed to length 6
but zero should come in front
any one there
plz reply
@ran
Is this a VARCHAR field?
UPDATE tab SET col = ‘00′ + col WHERE LEN(col) = 4;
@ran
UPDATE tab SET col = RIGHT(‘000000′ + col, 6)
Really great tip.
Thanks mate.