获取 SQL Server Image Text ntext数据长度

来源:互联网 发布:摩托分期付款怎么算法 编辑:程序博客网 时间:2024/06/05 05:03

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

MSSQLTips author Greg RobidouxBy: 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

  • Add this command to your SQL Server T-SQL toolbox
  • Take a look at these other useful T-SQL functions
    • SQL Server 2000 Text Data Manipulation
    • Date/Time Conversions Using SQL Server
    • Retrieving SQL Server Index Properties with INDEXPROPERTY
    • Retrieving SQL Server Database Properties with DATABASEPROPERTYEX


Last Update: 2/27/2007 
0 0
原创粉丝点击