0

SQL0802 Data conversion or data mapping error

The fields not being CAST are decimals. The fields I am trying to CAST are strings.

I have tried different variations or CAST and CONVERT on this case expression. I'm fairly certain this syntax is correct. I am still getting the error though.

CASE WHEN cpssn=amssn THEN amfnam||amlnam
     WHEN cpssn=CAST(maassn as DECIMAL(9)) THEN maafnm||maalnm
     WHEN cpssn=CAST(mpssno as DECIMAL(9)) THEN mppfnm||mpplnm 
END as Name
juergen d
  • 201,996
  • 37
  • 293
  • 362
madmike
  • 114
  • 1
  • 12

1 Answers1

0

One brute force method uses translate():

(CASE WHEN cpssn = amssn THEN amfnam||amlnam
      WHEN length(translate(massn, 'a0123456789', 'a')) > 0 THEN NULL
      WHEN length(translate(mpssno, 'a0123456789', 'a')) > 0 THEN NULL
      WHEN cpssn = CAST(maassn as DECIMAL(9)) THEN maafnm||maalnm
      WHEN cpssn = CAST(mpssno as DECIMAL(9)) THEN mppfnm||mpplnm 
 END) as Name

Note: I am not intimately familiar with translate() in DB2. The above uses an Oracle convention for removing characters, because the third argument cannot be '' in Oracle. It should still work in DB2.

This should work by first guaranteeing that the only characters in the strings are digits. case is processed in sequential order, so the digit checks should be done before the conversion.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786