on 01-19-2019 10:46 AM
I am working on one requirement where I need to take the maximum value of a column with string data type and increment by one everytime when there is a new entry in table.
For example.
Abc_current_date_5...
This should be incremented to Abc_current_date_6
The number part should be fetched from the table everytime and should be incremented by 1
How can we achieve this ?
Although I still believe that the design is wrong (as mentioned in comments), there are options to do that. For instance I created following scalar function which first splits the string in the "fix" part and in the number part. Then the number is increased and the result string with the increased number is created.
Function (the comment lines is the coding in case your HANA version does not already support executing the expressions/functions w/o doing it via a Select):
FUNCTION "increaseLastNumberInString" ( in iv_string nvarchar(5000 ) )
RETURNS ev_string nvarchar(5000)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
declare lv_regex nvarchar(256) := '\d+$';
declare lv_extract_fix_val nvarchar(5000);
declare lv_extract_num nvarchar(5000);
lv_extract_fix_val = substring(:iv_string, 1, locate_regexpr(:lv_regex in :iv_string) - 1);
--select substring(:iv_string, 1, locate_regexpr(:lv_regex in :iv_string) - 1) into lv_extract_fix_val from dummy;
lv_extract_num = substring_regexpr(:lv_regex in :iv_string);
--select substring_regexpr(:lv_regex in :iv_string) into lv_extract_num from dummy;
ev_string = :lv_extract_fix_val || (to_integer(:lv_extract_num) + 1);
--select :lv_extract_fix_val || (to_integer(:lv_extract_num) + 1) into ev_string from dummy;
END;
Some tests:
select "increaseLastNumberInString"( 'ABC_ID_20190101_0' ) from dummy; " returns ABC_ID_20190101_1
select "increaseLastNumberInString"( 'ABC_ID_20190101_40' ) from dummy; " returns ABC_ID_20190101_41
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.