Home > Back-end >  How to use REGEXP_LIKE() for concatenation in Oracle
How to use REGEXP_LIKE() for concatenation in Oracle

Time:10-28

I need to make some changes in SQL within a CURSOR. Previously, the maximum value for column 'code' was 4 characters (e.g. K100, K101,....K999) but now it needs to be 8 characters (e.g. K1000, K1001, K1002,....K1000000).

CURSOR c_code(i_prefix VARCHAR2)
IS
SELECT NVL(MAX(SUBSTR(code,2)) 1,100) code
FROM users
WHERE code LIKE i_prefix||'___';

The 'code' column value starts from 100 and increments 1 each time a new record is inserted. Currently, the maximum value is 'K999' and I would like it to be K1000, K1001, K1002 and so on.

I have altered and modified the 'code' column to VARCHAR(8) in the users table.

Note: i_prefix value is always 'K'.

I have tried to amend the SQL -

CURSOR c_code(i_prefix VARCHAR2)
IS
SELECT NVL(MAX(SUBSTR(code,2)) 1,100) code
FROM users
WHERE code LIKE i_prefix||'________';

However, it restarts from 100 and not from K1000, K1001, K1002, etc. each time a record is inserted.

I have been suggested to use REGEXP_LIKE() but not sure how to properly use it to get the desired outcome in this case.

Can you please guide me on how can we get this result using REGEXP_LIKE().

Thank you.

CodePudding user response:

Your old code

WHERE code LIKE i_prefix||'___';

will match K followed by exactly three characters, which is what you had. Your new code

WHERE code LIKE i_prefix||'________';

will match K followed by exactly eight characters, which is one too many for a start, since you said the total length was eigh - which means you need sever wilcard placeholders:

WHERE code LIKE i_prefix||'_______';

... but that still won't work at the moment since your existing values aren't that long. As all your current values are at least four, you could do:

WHERE code LIKE i_prefix||'___%';

which will match K followed by three or more characters - with no upper limit, but your column is restricted to eight too anyway.


If you did want to use a regular expression, which are generally slower, you could do:

WHERE REGEXP_LIKE(code, i_prefix||'.{3,7}');

which would match K followed by three to seven characters, or:

WHERE REGEXP_LIKE(code, i_prefix||'\d{3,7}');

which would only match K followed by three to seven digits.

fiddle


However, I would suggest you use a sequence to generate the numeric part, and just prefix that with the K character. The sequence could start from 100 on a new system with no data, or from the current maximum number in an existing system with data.

I would also consider zero-padding the data, including all the existing values, to allow them to be compared; so update K100 to K0000100. Or if you can't do that, once you get past K199 jump to K2000000. Either would then allow the values to be sorted easily as strings. Or, perhaps, add a virtual column that extracts the numeric part as a number.

  • Related