on 08-27-2018 2:26 PM
I have a table with a column LIST_OF_NUMBERS containing the following strings:
10, 20, 395, 443, 534, 734, 954, 105, 156
I want to truncate the LIST_OF_NUMBERS column to 10 characters as follows:
LEFT(LIST_OF_NUMBERS,10)
10, 20, 39
However, if a number from the list of string is partially truncated I want to truncate the whole number instead. For example in my case I do not want to display 39 as it's misinterpreting. I want to truncate the whole number as follows:
10, 20,
I believe it can be achieved with the following condition:
If the string does not ends with comma, truncate the strings until it ends with a comma.
How can I translate this condition in sql script?
Note that I am novice on creating store procedure.
One way to solve that is like following:
do
begin
-- prepare test data
lt_test = select '10, 20, 395, 443, 534, 734, 954, 105, 156' as list_of_numbers from dummy
union all select '10, 20, 36, 443, 534, 734, 954, 105, 156' as list_of_numbers from dummy
union all select '10, 20, 4, 443, 534, 734, 954, 105, 156' as list_of_numbers from dummy;
-- restrict to max 10 characters w/o cutting numbers in the middle
select case when length(list_of_numbers) > 10
then
case when locate_regexpr('[0-9]' in substring(list_of_numbers, 11, 1)) > 0
then
substring(list_of_numbers, 1, locate(left(list_of_numbers, 10), ',', -1))
else
left(list_of_numbers, 10)
end
else left(list_of_numbers, 10) end
from :lt_test;
end;
What does the script basically?
Regards,
Florian
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.