获取 SQL Server Image Text ntext数据长度

How to get length of Text, NText and Image columns in SQL Server

MSSQLTips author Greg Robidoux By: Greg Robidoux   |   Read Comments (4)   |   Related Tips: More > Data Types

Problem
There is sometimes a need to figure out the maximum space that is being used by a particular column in your database.  You would initially think that the LEN() function would allow you to do this, but this function does not work on Text, NText or Image data types, so how do you figure out the length of a value in a column that has one of these data types?

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.

Here is an example of running these commands on an IMAGE data type using the LEN function:

SELECT name, LEN(packagedata) 
FROM
 dbo.sysdtspackages

query results

 

Here is an example of running these commands on an IMAGE data type using the DATALENGTH function:

SELECT name, DATALENGTH(packagedata) 
FROM
 dbo.sysdtspackages

query results

 

If you wanted to find out the maximum length used for all of your records you could issue a command such as the following:

SELECT TOP 1 name, DATALENGTH(packagedata) 
FROM
 dbo.sysdtspackages 
ORDER BY
 2 DESC

 

That is all there is to it.  Another little function that you probably won't use often, but this is helpful to know it is there when you do need to use it.

Next Steps



Last Update: 2/27/2007 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值