cancel
Showing results for 
Search instead for 
Did you mean: 

Index Function

Former Member
0 Kudos

Hi,

I need to input certain values based on certain logic.

It is like if the 8'th character is empty or contains numeric then PART field contains value BCI and if 8'th character contains alphabet, then PART = 'DFI'

Below is the example:

If ID = 1234567 then PART = 'BCI'

ID = 12345678 then PART = ''BCI'

ID = 1234567AP01 then PART = 'DFI'

How do I write it using INDEX?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Naga,

Do you have access to the online technical manuals that are supplied with Data Services ? If not, I suggest you installl them because you will get much faster answers to these types of questions (usage of the built-in functions) from the manuals (particularly the reference guide) than you will from forums (and more accurate in some cases). The manuals are very well written and there are usually lots of examples.

Having said that, to guide you in the right direction the 'substr' function is probably going to be the one to look at since you know exactly which character you need to identify in the string (i.e. the 8 th). The 'index' function is more suited to cases where you are looking for a sequence of characters in a string and want to know at which position the first occurrence of the character sequence begins.

So basically, use 'substr' to get the 8th character inside a 'decode' function to compare this value to your three cases and determine the output.

Former Member
0 Kudos

Hi Clint.

I did try using decode and substring, but when i am writing the following code for '1234567A00', it is not working:

decode(substr( Test8char.VENDNUM,1,8) IN ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')), 'ABCD')

Former Member
0 Kudos

your decode statement needs a final input parameter, which is for the default expression, i.e. what to return if the previous conditions are all false, so for example :

decode(substr( Test8char.VENDNUM,1,8) IN 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')), 'ABCD',NULL)

Former Member
0 Kudos

Also, you can handle all three of your conditions in one decode statement because decode works like nested ifthenelse functions, eg. in this example from the manual:

decode ((EMPNO = 1), '111',

(EMPNO = 2), '222',

(EMPNO = 3), '333',

(EMPNO = 4), '444',

'NO_ID')

If EMPNO=1 it returns '111', else it checks the next condition,

If EMPNO=2 it returns '222', else it checks the next condition,

If EMPNO=3 it returns '333', else it checks the next condition,

If EMPNO=4 it returns '444', else it returns the default expression 'NO_ID'