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.
This is the most helpful thing EVER. Thank you!
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..?
Note that there is no such datatype as text(max)
How did you insert data?
Can you post the code?
Are you use access database?
if yes then take data type memo.
if you use sqlserver 2005/08 there are no text data type
there you can use nvarchar(max)
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!
When the value is NULL, the size is also NULL
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
It is formation issue. If you use front end application do the formation there
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.
Hi! This is not the first time your posts help me. Thank you so much!
Thanks a lot for such a useful tip.
Please help.
Need to add/combine 3 columns
Column1 column column3
eddie lewis 125.87456
query is select :
column1+column2+cast(column3/100 as varchar(20))
returns – eddielewis1.25874 i.e missing last 2 digits of column3 …… please help
Thanks
Hey Andazi,
Change the query to
column1+column2+cast(column3/100 as varchar(50))
As the cast is only getting the first 20 characters
THIS HELPED ME SO MUCH – THANK YOU!!!
You guys explained how to change the data amount being returned directly in SQL server, but what about the amount being returned in an ASP page?
This is great!
But how can I find out the MAXSIZE of the field in question?
We have recently updated our DB to increase the size of certain fields. I need to check if the user updated their DB before performing any work on those fields.
Hello,
I have a table which has field1, field2 and field3 columns of type int, the combination of those columns represent 10 digit phone number. I know field3 always have 4 digits, but MS SQL trims the right most 0s. all i am tring to acheive is to provide the user with one field 10 digit number.
here is my query:
INSERT INTO [Info Direct Res new]
SELECT ‘Info Direct’, /* Datbase Name */
DECLARE @MAX_FIELD_LEN int;
DECLARE @fieldLength int;
SET @MAX_FIELD_LEN=4;
SET @fieldLength = len(Cast(field3 as varchar));
cast(field1 as varchar)+cast(field2 as varchar)+RIGHT(’0′,@MAX_FIELD_LEN-@fieldLength)+Cast(field3 as varchar), /* Phone Number */
field5, /*First name*/
field6, /*Last Name*/
ISNULL(field8,”) + ‘ ‘ + ISNULL(field9,”)+ ‘ ‘ + ISNULL(field10,”)+ ‘ ‘ + ISNULL(field11,”)+ ‘ ‘ + ISNULL(field12,”), /* Full Address */
field13, /* city */
field14, /* province */
field15, /* postal code */
‘ ‘
from stcir1001
I get syntax error where i say declare
thanks,
Just what I needed, thank you
I have a tabe in oracle which has a varchar field with length of 48. I need to select records that are not in the length of either 8, 15 & 16 size.
The second criteria is to identify records which are not numeric i.e. any character value should be displayed.
Thanks Alot.
I got what i wanted since many days.
I was trying LENGTH bat it wasnt working.
Thanks alot once again.
My SQL related googling always leads me to your blog, and your articles are usually pretty helpful. Thanks!
The msdn website shows that len and datalength functions are different.
LEN- Returns the number of characters of the specified string expression, excluding trailing blanks.
DATALENGTH – Returns the number of bytes used to represent any expression.
Thanks a lot!
I want to increase the text size in one specific DB. I see that the maximum text field size I already have is around 32,000 characters but text with 40,000 characters was truncated.
How can i increase text size in specific DB or table?
Why do you need this? If you want to view full content, export to text file and view from there
The problem is not to VIEW the content BUT to insert the whole text. the text is not inserted completely and is truncated…
Sir can please you let me know that what is difference between LEN and DATALENGTH
Thank you!
Hello All
I want to fetch only 100 character from spacific column, plz tell me what is query for that.
select left(col,100) from table
I am sending the output of a query to a file and unfortunately every row is containing only a certain number of characters. My query has rows having length more than 10000 characters for each row. When I get the query and send it to a file I am seeing only the first 250 characters for each row instead of 10000 characters.
I will be grateful if anyone can help me suggesting a solution for this problem.
I appreciate your help in advance.
i tried this option which is not helping in my case as the max it support is 8192
Go to Tools –> Options –> Results tab. Change the “Maximum characters per column” The default is 256
jaheer,
Not sure this helps but in SQL Server 2008 the maximum number of bytes per row is 8,060. This restriction is relaxed for tables with varchar, nvarchar, varbinary, or sql_variant columns that cause the total defined table width to exceed 8,060 bytes. The lengths of each one of these columns must still fall within the limit of 8,000 bytes, but their combined widths may exceed the 8,060 byte limit in a table. For more information, see Row-Overflow Data Exceeding 8 KB in BOL
ANOTHER USE OF LEN() AND DATALENGTH() ARE :-
SELECT LEN(‘SRI ‘) – 3
SELECT DATALENGTH(‘SRI ‘) – 9
LEN() IGNORES THE SPACES BUT NOT DATALENGTH()
Wow! Sooo simple. Used it. Loves it.
pking,
Thanks for the reply, i have found the solution for the issue.it simple we need to use the below command from sqlcmd to fetch a row having column with more than 10000 characters.
sqlcmd -S 10.11.111.121 -U sa -P abc@123 -d DBNAMe -i ./conf/SQL.Archive_query.conf.sql -o ./tmp/sqlserver/SDS_Archive_Update.ddl -y 0
-y 0 –> Using this option will increase the length of a row
How can i find the length of the memo field (ntext). through sql query.
The len() function is not working for memo datatypes.
kindly suggest
Thanks
Amit
Amit, try using the command DATALENGTH().
Really nice
Thank you for having the perfect answer. I always come here first, as I have found most of what I have ever questioned answered here.
i confirm, write to target…thanks
What is the best way to set up the table structure in SQL if I do not know the maximum length of all the fields?
for varcha use max, for numbers use BIGINT, etc
thanks!
I was just wondering how to test if a column of text type is null
This helps me a lot !!
thanks
how do I get the len of a field content where fieldname is in a variable
Follwing SQL does not return the max length of the content.
ex: Declare @f1 as char(50);
set @f1=”City”
select max(len(@f1)) from
Thank you. Simple but this is the answer…
thanks you..
Hello
How can we split a column which has arabic and english text.
Thanks! Works perfectly