1

I have a SQL Server 2012 database which users can attach files to (mainly Jpgs) however the table size is 130mb with only 20 records.

I need to find out which row is using the most amount of space.

The data is stored as varbinary

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SCramphorn
  • 447
  • 4
  • 23

1 Answers1

2

You can use DATALENGTH to get the length of the varbinary column:

SELECT Id, Name, DATALENGTH(BinaryColumn)
FROM dbo.YourTable
ORDER BY DATALENGTH(BinaryColumn) DESC
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thanks, perfect. How can I convert this to kb / mb - * 8 / 1024? `select ((DATALENGTH(filecontent) * 8) / 1024) fg, * from Attachments order by fg desc` – SCramphorn Jun 06 '17 at 14:21
  • @SCramphorn: `DATALENGTH` returns **number of bytes** - so just divide by 1024 (or 1024.0, if you want to avoid integer division) or by (1024*1024) etc. – marc_s Jun 06 '17 at 14:22