SQL SERVER – Find Length of Text Field

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 (https://blog.sqlauthority.com)

SQL Function, SQL Scripts
Previous Post
SQLAuthority.com News – Journey to SQL Authority Milestone of SQL Server
Next Post
SQL SERVER – Retrieve Current DateTime in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

Related Posts

84 Comments. Leave new

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

    Reply
  • Good work !!

    Reply
  • Good tips!!!
    Thanks bro

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

    Reply
  • any one there
    plz reply

    Reply
  • @ran

    Is this a VARCHAR field?

    UPDATE tab SET col = ’00’ + col WHERE LEN(col) = 4;

    Reply
  • @ran

    UPDATE tab SET col = RIGHT(‘000000’ + col, 6)

    Reply
  • Really great tip.
    Thanks mate.

    Reply
  • Hi! This is not the first time your posts help me. Thank you so much!

    Reply
  • Thanks a lot for such a useful tip.

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

    Reply
    • Hey Andazi,
      Change the query to

      column1+column2+cast(column3/100 as varchar(50))

      As the cast is only getting the first 20 characters

      Reply
  • THIS HELPED ME SO MUCH – THANK YOU!!!

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

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

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

    Reply
  • Just what I needed, thank you

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

    Reply
  • Thanks Alot.
    I got what i wanted since many days.
    I was trying LENGTH bat it wasnt working.
    Thanks alot once again.

    Reply
  • My SQL related googling always leads me to your blog, and your articles are usually pretty helpful. Thanks!

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

    Reply

Leave a Reply