Sql到postgresql

Sql到postgresql,sql,postgresql,Sql,Postgresql,我有一个我正在使用的sql代码。对postgresql不太熟悉。下面是我想转换成postgresql的代码。我正在使用dbeaver 3.5.4 Update tablename set record_hash = cast(hashbytes('MD5', coalesce(id, '') + coalesce(name, '') + coalesce(created_date, '')

我有一个我正在使用的sql代码。对postgresql不太熟悉。下面是我想转换成postgresql的代码。我正在使用dbeaver 3.5.4

Update tablename
set record_hash = cast(hashbytes('MD5',
                  coalesce(id, '') +
                  coalesce(name, '') +
                  coalesce(created_date, '') +
                  coalesce(last_modified_date, '')
                  ) as bigint) 
;

您可以这样做:

Update tablename
set record_hash = ('x'|| substr(
                            md5(
                              coalesce(id, '') ||
                              coalesce(name, '') ||
                              coalesce(created_date, '') ||
                              coalesce(last_modified_date, '')
                            ),1,16)::bit(64)::bigint )

找到了如何进行hash=>bigint转换。

我假设这个
hashbyte()
东西会生成一个hash值

要在Postgres中创建md5校验和,可以使用md5()函数,例如:

md5(concat(id::text, name, created_date::text, last_modified_date::date))
concat()
将自动处理空值,无需
coalesce()


不幸的是,Postgres中没有从十六进制值到整数的直接转换,Postgres将MD5作为内置函数:

Update tablename
    set record_hash = ('x' || lpad(md5(coalesce(id, '') ||
                                       coalesce(name, '') ||
                                       coalesce(created_date, '') ||
                                       coalesce(last_modified_date, '')
                                      ), 16, '0'
                                  )
                      )::bit(64)::bigint;

对于转换回
bigint
,请在需要信用的地方给予信用。Erwin Brandstetter的回答通常非常详尽,因此我希望它能很好地工作。

正如信息一样,这里有几种方法可以在不丢失数据的情况下表示16字节的数据:

with t(x) as (values(md5('abc123')))
select
  t.x, -- varchar(32), char(32)
  t.x::uuid, -- uuid
  ('x'||t.x)::bit(128), -- exactly bit(128)
  ('\x'||t.x)::bytea, -- bytea
  array[('x'||left(t.x,16))::bit(64)::bigint, ('x'||right(t.x,16))::bit(64)::bigint] -- bigint[]
  -- ... and so on
from t;

Postgres也在使用SQL.Dbeaver是一个SQL客户机,用于几个使用EclipseIDE作为基础的数据库。它连接到哪个数据库?您可以在数据库导航视图中看到这一点,然后单击“编辑连接”。它在窗口的顶部。只是想澄清一下:您是否想将16字节md5哈希存储到8字节
bigint
字段中?更不用说我在这里找到的是SQL Server Nelson Teixeira:我正在使用amazon redshift。Abelisto:是否要存储16字节md5哈希lpad是否缺少第二个参数?@NelsonTeixeira。谢谢。非常感谢您的回复。我使用了这个查询,得到了DBCException:SQL错误[42846]:错误:无法将类型文本转换为位。。似乎是一个铸造问题。。record_hash设置为bpchar..不确定应该为此更改什么数据类型。非常感谢您的回复。我使用了这个查询,得到了DBCException:SQL错误[42846]:错误:无法将类型文本转换为位。。似乎是一个铸造问题。。记录\u散列设置为bpchar..不确定我应该为此更改什么数据类型记录\u散列字段的类型是什么?现在,我将其更改为数字,但出现相同的错误。尝试将其更改为bigint,因为它是要强制转换的类型。:)让它工作起来。若我删除位64,那个么update语句正在工作。谢谢你的耐心。你真是帮了大忙!!