【问题标题】:Convert the MD5 output into 32 bit integer in Redshift在 Redshift 中将 MD5 输出转换为 32 位整数
【发布时间】:2016-02-22 11:53:08
【问题描述】:

我在 Redshift 中尝试过以下操作

SELECT STRTOL(MD5('345793260804895811'), 10);

但我得到了以下DBCException

SQL 错误 [22023]:错误:输入 cf82576a6dbf9ff63cf9828f990f0673 无效,无法转换为基数 10

org.postgresql.util.PSQLException: PSQLException: ERROR: 输入 cf82576a6dbf9ff63cf9828f990f0673 无效,无法转换为基数 10

如何在 Redshift 中完成这项工作?

【问题讨论】:

  • MD5 函数不返回数值。它是 char(32) 值,不能转换为数字。您需要其他函数来返回 bigint 哈希(Redshift 没有内置这种函数)

标签: amazon-redshift


【解决方案1】:

你有两个问题:

  • 首先,您需要将转换指定为基数 16
  • 其次,一个MD5字符串会大量溢出一个64位的BIGINT

这很好用

SELECT STRTOL(LEFT(MD5('345793260804895811'),15), 16);

将 MD5 十六进制值缩短为最左边 15 个字符,并使用基数 16 转换为 BIGINT

【讨论】:

    【解决方案2】:

    我想出这个来将 MD5 存储在两个 BIGINT 字段中,而不是 CHAR(32) - 节省 2 倍空间!

    select 
        convert(bigint,
            strtol(substring(hash,1,8),16) * 4294967296.0 +
            strtol(substring(hash,9,8),16) - 9223372036854775807
        ) as hash_part1
        ,convert(bigint,
            strtol(substring(hash,17,8),16) * 4294967296.0 +
            strtol(substring(hash,25,8),16) - 9223372036854775807
        ) as hash_part2
    

    希望对某人有所帮助。

    【讨论】:

      【解决方案3】:

      MD5 的结果是 128 位长(ref),你不能把它装进一个 32 位整数。

      【讨论】:

        【解决方案4】:

        您可以尝试以 16 而非 10 为基数进行转换:

        SELECT STRTOL(MD5('cf82576a6dbf9ff63cf9828f990f0673'), 16);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-25
          • 2012-10-26
          • 2015-05-18
          • 2019-05-30
          相关资源
          最近更新 更多