How to md5 all columns regardless of type

前端 未结 4 2145
抹茶落季
抹茶落季 2021-02-08 11:40

I would like to create a sql query (or plpgsql) that will md5() all given rows regardless of type. However, below, if one is null then the hash is null:

UPDATE t         


        
相关标签:
4条回答
  • 2021-02-08 12:00

    There is much more elegant solution for this.

    In Postgres, using table name in SELECT is permitted and it has type ROW. If you cast this to type TEXT, it gives all columns concatenated together in string that is actually JSON.

    Having this, you can get md5 of all columns as follows:

    SELECT md5(mytable::TEXT)
    FROM mytable
    

    If you want to only use some columns, use ROW constructor and cast it to TEXT:

    SELECT md5(ROW(col1, col2, col3)::TEXT)
    FROM mytable
    

    Another nice property about this solution is that md5 will be different for NULL vs. empty string.

    Obligatory SQLFiddle.

    0 讨论(0)
  • 2021-02-08 12:00

    Have you tried using CONCAT()? I just tried in my PG 9.1 install:

    SELECT CONCAT('aaaa',1111,'bbbb');     => aaaa1111bbbb
    SELECT CONCAT('aaaa',null,'bbbb');     => aaaabbbb
    

    Therefore, you can try:

    SELECT MD5(CONCAT(column1, column2, column3, column_n))    => md5_hash string here
    
    0 讨论(0)
  • 2021-02-08 12:08

    select MD5(cast(p as text)) from fiscal_cfop as p

    0 讨论(0)
  • 2021-02-08 12:18

    You can also use something else similar to mvp's solution. Instead of using ROW() function which is not supported by Amazon Redshift...

    Invalid operation: ROW expression, implicit or explicit, is not supported in target list;

    My proposition is to use NVL2 and CAST function to cast different type of columns to CHAR, as long as this type is compatible with all Redshift data types according to the documentation. Below there is an example of how to achieve null proof MD5 in Redshift.

    SELECT md5(NVL2(col1,col1::char,''), 
               NVL2(col2,col2::char,''), 
               NVL2(col3,col3::char,''))
    FROM mytable
    

    This might work without casting second NVL2 function argument to char but it would definately fail if you'd try to get md5 from date column with null value. I hope this would be helpful for someone.

    0 讨论(0)
提交回复
热议问题