How to md5 all columns regardless of type

前端 未结 4 2143
抹茶落季
抹茶落季 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: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.

提交回复
热议问题