Question

DB2 V9 z/os

Background: I have a 4 column table defined as (col1 int, col2 smallint, col3 int, col4 date)

Row 1 has values of (1,123,456,2012-08-23)

When I execute the following:

SELECT CAST(col2 AS VARCHAR(5)) CONCAT CAST(col3 AS VARCHAR(5))
  FROM db.T1 
 WHERE col1 = 1;

Value 123456 is returned, which is exactly what i want.

When I execute the following:

UPDATE db.table2
   SET col3 = SELECT CAST(col2 AS VARCHAR(5)) CONCAT CAST(col3 AS VARCHAR(5))
  FROM db.T1 
 WHERE col1 = 1;

Error is:

SQL0408N A value is not compatible with the data type of its assignment target. Target name is "col3". SQLSTATE=42821

I understand the error is due to attempting to insert a varchar into an integer. What else can I do? I've tried using various CAST statements but cannot get a value to insert into col3. i need the value to appear joined as shown above.

Any help would be appreciated.

Was it helpful?

Solution

Wrapping all of the casts as a final cast( ... as integer) should work:

UPDATE db.table2 
SET col3 = SELECT CAST(
        CAST(col2 AS VARCHAR(5)) CONCAT CAST(col3 AS VARCHAR(5)) 
    AS INTEGER)
FROM db.T1 
WHERE col1 = 1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top