返回任何表達式所佔用的字節數。數據庫
DATALENGTH ( expression )
express
expressionpost
任何類型的表達式。ui
intspa
DATALENGTH 對 varchar、varbinary、text、image、nvarchar 和 ntext 數據類型特別有用,由於這些數據類型能夠存儲可變長度數據。code
NULL 的 DATALENGTH 的結果是 NULL。blog
說明 兼容級別可能影響返回值。有關兼容級別的更多信息,請參見 sp_dbcmptlevel。圖片
此示例查找pubs
表中 pub_name 列的長度。ip
USE pubs
GO
SELECT length = DATALENGTH(pub_name), pub_name
FROM publishers
ORDER BY pub_name
GO
下面是結果集:io
length pub_name
----------- ----------------------------------------
20 Algodata Infosystems
16 Binnet & Hardley
21 Five Lakes Publishing
5 GGG&G
18 Lucerne Publishing
14 New Moon Books
17 Ramona Publishers
14 Scootney Books
--佔用存儲空間列表
select d.WarehouseName,c.DepartmentName,b.EquipmentNo,b.ToolName, DATALENGTH(a.Picture)/1024 as Length_KB, DATALENGTH(a.Picture)/1024/1024 as Length_MB from T_ToolPic a
inner join t_tool b on a.ToolID = b.ToolID
inner join T_Department c on b.DepartmentID = c.DepartmentID
inner join T_Warehouse d on b.WarehouseID = d.WarehouseID
order by DATALENGTH(a.Picture) desc,c.DepartmentName,d.WarehouseName
--圖片佔用數據庫存儲空間
select sum(DATALENGTH(a.Picture)/1024)/1024 as Length_MB from T_ToolPic a
inner join t_tool b on a.ToolID = b.ToolID
inner join T_Department c on b.DepartmentID = c.DepartmentID
inner join T_Warehouse d on b.WarehouseID = d.WarehouseID