Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data length in ntext column?

Tags:

sql-server

How do you find out the length/size of the data in an ntext column in SQL? - It's longer than 8000 bytes so I can't cast it to a varchar. Thanks.

like image 288
Nick Avatar asked Oct 23 '08 15:10

Nick


People also ask

How do I get the length of a Ntext field in SQL?

Solution. To get the length of an ntext field, use DATALENGTH() instead of LEN() . Note that DATALENGTH will return the number of bytes, not the number of characters in the string. Each character in an ntext field is 2 bytes, so you need to take this into account when writing your query.

How can you capture the length of a column when it is a text Ntext and or image data type?

Solution. In addition to the LEN() function, SQL Server also has a DATALENGTH() function. This function can be used on all data types in your table. That's all there is to it.

How do I get the length of a data column in SQL?

Use COL_LENGTH() to Get a Column's Length in SQL Server In SQL Server, you can use the COL_LENGTH() function to get the length of a column. More specifically, the function returns the defined length of the column, in bytes. The function accepts two arguments: the table name, and the column name.

How do I find the data type and length of a column in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.


3 Answers

Use DataLength()

SELECT * FROM YourTable WHERE DataLength(NTextFieldName) > 0  
like image 147
korro Avatar answered Oct 10 '22 13:10

korro


The clue's in the question: use DATALENGTH(). Note it has a different behaviour to LEN():

SELECT LEN(CAST('Hello   ' AS NVARCHAR(MAX))), 
       DATALENGTH(CAST('Hello   ' AS NVARCHAR(MAX))), 
       DATALENGTH(CAST('Hello   ' AS NTEXT))

returns 5, 16, 16.

In other words, DATALENGTH() doesn't remove trailing spaces and returns the number of bytes, whereas LEN() trims the trailing spaces and returns the number of characters.

like image 33
onedaywhen Avatar answered Oct 10 '22 13:10

onedaywhen


Select Max(DataLength([NTextFieldName])) from YourTable
like image 21
Steve D Avatar answered Oct 10 '22 12:10

Steve D