Convert HEX value to CHAR on DB2

前端 未结 3 1633
心在旅途
心在旅途 2020-12-19 18:06

In connection with data replication from SQL Server to DB2 I have the following question:

On DB2 I have a table containing (fo

相关标签:
3条回答
  • 2020-12-19 18:16

    DB2 has built-in encoding/decoding.

    For OPs question, use....

    select CAST(ColumnName as char(20) CCSID 37) as ColumnName from TableName where SomeConditionExists
    

    http://www-01.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.intro/src/tpc/db2z_introcodepage.dita

    0 讨论(0)
  • 2020-12-19 18:17
    select hex('A') from sysibm.sysdummy1; 
    

    returns 41. and

    select x'41' from sysibm.sysdummy1;
    

    gives you 'A'. So you can put that in a for loop and loop through each pair of hex characters to arrive at your original string. Or you can write your own unhex function.

    Taken from dbforums.com /db2/1627076-display-hex-columns.html (edit Nov 2020: original source link is now a spam site)

    0 讨论(0)
  • 2020-12-19 18:25

    This is one of most close topics to subject of my problem:

    I have lost 2 days to figure out how to migrate XML files stored in DB2 BLOB field using SQL Developer. (Yes, migrating to and doing the queries from SQL Developer - we are migrating data to Oracle from DB2, so we were using this tool)!

    How to show XML file/string stored in BLOB?

    Let's start with, what the problem was:

    1. Data in BLOB was a XML file.
    2. When selected in query, got:

    3. When casted, like:

    select CAST(BLOBCOLUMN as VARCHAR(1000)) from TABLE where id = 100;

    output was in HEX:

    1. Nothing worked... Not even the solution from the links in this topic. !NOTHING!

    By mistake found a solution:

    1. CREATE FUNCTION in DB2:

      CREATE FUNCTION unhex(in VARCHAR(32000) FOR BIT DATA) RETURNS VARCHAR(32000) LANGUAGE SQL CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION BEGIN ATOMIC RETURN in; END

    2. Run SELECT:

      select UNHEX( CAST(BLOBCOLUMN as VARCHAR(32000) FOR BIT DATA)) from TABLE where id = 100;

    3. Result:

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