将十六进制字段转换为每日可用性(通常和在T-SQL中)

时间:2014-07-23 20:43:04

标签: sql hex

我最近开始尝试从一个我不太熟悉的旧数据库中获取一些数据。它具有的一个功能是每日可用性列,它可以表示资源在一天内可用。不幸的是,该字段是十六进制字段,如下所示:

0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

我相信这代表上午6点到下午4点半,我不确定怎么样,这不是我的强项。似乎每15个字符代表一个小时,唯一令人困惑的部分是4:30结束时的3F。

所以问题是我如何在SQL中操作/解析这一点,以便在弄清楚数据的格式化后获取开始值和结束值?

以下是更多样本数据:

0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0700000000000000

这个代表下午4:29 - 晚上10:59

0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F00000000000000

这是下午4:30 - 晚上11:00

0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000000000

这个代表下午4:30 - 晚上10:00

1 个答案:

答案 0 :(得分:1)

你的数学分解如下:

  1. 一小时3600秒
  2. 15个字符代表一小时
  3. 3600/15 =每个角色240秒
  4. 十六进制F = 16. 240/16 =每个数字15秒。
  5. 原来,它一次需要2位数,这就是为什么你看到0C和F0的原因。

    这需要更多的润色,但它一直都是我的。

    declare @binaryS binary(180)
    set @binaryS = 0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0700000000000000
    declare @s varchar(max)
    set @s = Convert(varchar(max), @binaryS, 2)
    declare @seconds int
    set @seconds = 0
    declare @i int
    set @i = 1
    declare @t datetime
    set @t = Convert(datetime, '01/01/2014', 101)
    WHILE (@i <= 360)
    BEGIN
        declare @segment varchar(max)
        set @segment = substring(@s, @i, 2)
        --print '0x' + @segment
        set @seconds = (convert(int, convert(varbinary, '0x' + @segment, 1))) * 15
        set @t = DATEADD(ss,480,@t)
        --print @seconds
        if(@seconds > 0)
          print @t
        set @i = @i + 2
    END
    

    快速解释。 @binaryS是您的db值。它转换为字符串,然后一次解析2位数。对于循环的每次迭代,将480秒(FF)添加到日期。当@seconds不为零时,它会打印出时间。因此,结果是每4分钟启用一次。您需要捕获开始和结束时间中的第一个和最后一个。以上工作在SSMS查询窗口中,但SQLFiddle无法处理它。