469,647 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

How to translate string with data have integer + character and null data

Hi,

I am new using DB2 9.1 database by windows base.
I want to query the data that contain string then translate the string into integer using DB2.
The problems is If the data is null, i got the problem to translate.
How to translate string also allow null data to integer. If null data it will read as space.

My Data :-

Expand|Select|Wrap|Line Numbers
  1. GEOSEG_ID     SEQNO      
  2. ----------  ---------
  3. 329802           2
  4. 329803           3A
  5. 329805           1A
  6. 329806          10
  7. 329808          11A
  8. 329810           9
  9. 329811           4
  10. 329812           6
  11. 329813           5
  12. 329814           7
  13. 329815           8A
  14. 329843          13A
  15. 329844          20
  16. 329845          21
  17. 329846          19
  18. 329848          14
  19. 329849          16
  20. 329850          15
  21. 329851          22
  22. 329852          18
  23. 329854          24
  24. 329855          23
  25. 329868         NULL
  26. 329869        NULL
  27. 329870         NULL
  28. 329871         NULL
  29. 329872         NULL
  30. 329873         NULL
This is My Query :-
-----------------
Expand|Select|Wrap|Line Numbers
  1. Select geoseg.geoseg_id,CAST(LTRIM(RTRIM(TRANSLATE(Elot_detail1.sequence, ' ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))) AS INTEGER) AS seqNo
  2. FROM  GEOSEG,ELOT_DETAIL1 
  3. WHERE    ELOT_DETAIL1.GEOSEG_ID  = GEOSEG.GEOSEG_ID
  4. ELOT_DETAIL1.POSTCODE  = '41200'
  5. AND    ELOT_DETAIL1.BIT  = '41'  
  6. GROUP BY  GEOSEG.GEOSEG_ID,Elot_detail1.sequence  
Sql error will appear like this :-
---------------------------------

SQL0420N Invalid character found in a character string argument of the
function "INTEGER". SQLSTATE=22018

SQL0420N Invalid character found in a character string argument of the function "INTEGER ".

Explanation:

The function "<function-name>" has a character string argument
that contains a character that is not valid in a numeric SQL
constant. The function may have been called as a result of using
the CAST specification with "<function-name>" as the target data
type. The function or data type used in the SQL statement may be
a synonym for "<function-name>".

If a decimal character is specified in the DECIMAL function then
that is the character that must be used in place of the default
decimal character.

User Response:

Ensure that the character strings that are being converted to
numeric types contain only characters that are valid in numeric
SQL constants, using the decimal character, if specified.

sqlcode : -420

sqlstate : 22018
Mar 21 '08 #1
3 11511
sakumar9
127 Expert 100+
I am not able to understand why do you want this conversion from string to integer?

You cannot cast any string into integer......... they are not compatible....... i mean it doesn't makes any sense to me atleast.......

By the way, the error which you are getting is because of CAST function that you are using. CAST is trying to convert a string into an INTEGER which is not compatible(string and integer).

If you can explain what you really want to achieve, that would be great !

Regards
-- Sanjay
Mar 21 '08 #2
Sorry because not explain well.

Actually i want to translate the sequence it will change string that have character to space "".

Like 1A it will read 1 and space.

example :-
--------------
CAST(LTRIM(RTRIM(TRANSLATE('1A', ' ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))) AS INTEGER)

This query will done perfectly.

But if the data NULL, it will got error. How to make this query also can read NULL data and change it to space.
Mar 21 '08 #3
docdiesel
297 Expert 100+
Hi,

try a

Expand|Select|Wrap|Line Numbers
  1. CASE WHEN NOT (SEQNO IS NULL)
  2. THEN
  3.   insert your cast statement
  4. ELSE
  5.  
  6. END;
Or maybe you'd like to leave it NULL instead of turning it to zero (0) in the ELSE part. Does this work for you?

Regards,

Bernd
Mar 22 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Don Starr | last post: by
33 posts views Thread by Jordan Tiona | last post: by
7 posts views Thread by elliotng.ee | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
3 posts views Thread by jacob navia | last post: by
14 posts views Thread by rtillmore | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.