Solved

T-SQL: max lenth of text in nvarchar(4000)

Posted on 2013-10-28
4
699 Views
Last Modified: 2013-11-05
Hi, how to determine maximum lenth of existing text in SQL Server table nvarchar(4000) column?
Thanks.
0
Comment
Question by:quasar_ee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 39606455
SELECT  MAX( DATALENGTH(ColumnName) )  from yourTable
SELECT  MAX( LEN(ColumnName) )  from yourTable
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39607977
DATALENGTH is especially useful with varchar, varbinary, text, image, nvarchar, and ntext data types because these data types can store variable-length data.
datalength() is the number of bytes involved (including trailing white space if any)

Whereas len() is the number of characters (but one character can be multi-byte) and it  excludes trailing blanks.
0
 
LVL 7

Expert Comment

by:luani
ID: 39608209
you can use ltrim, rtrim of you dont wont to count spaces

SELECT  MAX( LEN(ltrim(rtrim(ColumnName))) )  from yourTable
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39608248
luani wrote:

you can use ltrim, rtrim of you dont wont to count spaces

That would only strip out leading and/or trailing spaces, and will leave intact any internal spaces.

That said, if the concern is for trailing spaces, then as PortletPaul already indicates, LEN() already leaves those out...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question