SQL SERVER – Find Length of Text Field

To measure the length of VARCHAR fields the function LEN(varcharfield) is useful. For the length of text field data, you need DATALENGTH instead.

SQL SERVER - Find Length of Text Field

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

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Function, SQL Scripts
Previous Post
SQL SERVER – Delay Function – WAITFOR clause – Delay Execution of Commands
Next Post
SQL SERVER – Retrieve Current DateTime in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

Related Posts

84 Comments. Leave new

  • Really useful

    Reply
  • Just what I wondered about.

    Reply
  • 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

    Reply
  • Thnx!!
    This was just what I needed
    xx

    Reply
  • 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!

    Reply
  • This is what I was looking for.
    Thanks

    Reply
  • Andrew Cullis
    March 28, 2008 8:07 pm

    Nice …

    but now how would i do that in Hibernate …

    : )

    Reply
  • Thanks for the info! Just what I was looking for…

    Reply
  • Thanks :) You saved me lots of trouble…

    Reply
  • I need some statement to check database field size in the code before inserting any value

    Reply
  • Imran Mohammed
    July 17, 2008 7:46 am

    @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.

    Reply
  • thanks , very useful

    Reply
  • Thanks! You are the SQL man!

    Reply
  • Vey helpful dude!! Keep up the good work!
    Jai Hind!

    Reply
  • Thank you sir! Just what I was looking for!

    Reply
  • Thanks. Short and usefull info.

    Reply
  • Really useful…

    Reply
  • Ramani Sandeep
    April 16, 2009 8:59 pm

    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..?

    Reply
    • Note that there is no such datatype as text(max)

      How did you insert data?

      Can you post the code?

      Reply
      • 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.

    Reply
  • Imran Mohammed
    July 6, 2009 11:51 am

    @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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.