SQL Server獲取TEXT字段的內容長度

DATALENGTH

返回任何表達式所佔用的字節數。數據庫

語法

DATALENGTH expression )express

參數

expressionpost

任何類型的表達式。ui

返回類型

intspa

註釋

DATALENGTH 對 varcharvarbinarytextimagenvarchar  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

相關文章
相關標籤/搜索