Calculating the maximum size of BLOB in the BLOB column in SQL Server 2016 database

mjqavswn  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(107)

I have a table Table1 with large amounts of BLOB data in a column blob_column2 .

I want to calculate the maximum size of the blob (in KBs or MBs) stored in this column.

I need this input for migrating the data.

Can someone help with the query to calculate the maximum size of the blob?

The SQL Server database version is 2016 (v13.0).

kg7wmglp

kg7wmglp1#

The question is badly worded and since no attempt is shown, one can only guess what's being asked

You may be asking how to find the larges (maximum) blob size. Finding the maximum is done with MAX :

SELECT MAX(DATALENGTH(BlobField)) 
FROM ThatTable

That's not very useful for migration, unless you migrate to a database where blobs have length restrictions like MySQL, where BLOB is just 65KB. The equivalent to varchar(max) is LONGBLOB . There may be more issues from missing features like FILESTREAM storage.

Or you may be asking for the total size of all BLOBs, which is useful in storage calculations:

SELECT SUM(DATALENGTH(BlobField)) 
FROM ThatTable

You can calculate multiple statistics at once:

SELECT 
    SUM(DATALENGTH(BlobField)) as TotalSize, 
    SUM(DATALENGTH(BlobField))/1024.0 as TotalSizeKB, 
    MAX(DATALENGTH(BlobField)) as MaxSize,
    MAX(DATALENGTH(BlobField))/1024.0 as MaxSizeKB,
    AVG(DATALENGTH(BlobField)) as AvgSize,
    AVG(DATALENGTH(BlobField))/1024.0 as AvgSizeKB,
FROM ThatTable

Keep in mind that everyone except storage vendors defines 1 KB=1024 bytes. That includes file system creators. Storage vendors define it as 1000 bytes

相关问题