31

我们有一个将文件存储为 varbinary(MAX) 的 db 表。当我们运行以下脚本时:

SELECT SUM(LEN(Content)) FROM dbo.File

结果是:

35398663

我想把这个数字转换成兆字节?这可能吗?

4

1 回答 1

74

用于DATALENGTH检索字节数,然后进行转换,如下所示:

SQL小提琴

MS SQL Server 2017 架构设置

CREATE TABLE supportContacts 
    (
     id int identity primary key, 
     type varchar(20), 
     details varchar(30)
    );

INSERT INTO supportContacts
(type, details)
VALUES
('Email', 'admin@sqlfiddle.com'),
('Twitter', '@sqlfiddle');

查询 1

select *, gigabytes / 1024.0 as terabytes
from (
  select *, megabytes / 1024.0 as gigabytes
  from (
    select *, kilobytes / 1024.0 as megabytes
    from (
      select *, bytes / 1024.0 as kilobytes
      from (
        select sum(datalength(details)) as bytes
        from supportContacts       
      ) a
    ) b  
  ) c
) d

结果

| bytes | kilobytes |     megabytes |      gigabytes |         terabytes |
|-------|-----------|---------------|----------------|-------------------|
|    29 |   0.02832 | 0.00002765625 | 2.700805664e-8 | 2.63750553125e-11 |
于 2012-09-14T16:44:41.310 回答