简体   繁体   中英

SAP HANA SQL String functions

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 option is a long case when statement. It will look at the 11th character, if it is a comma, we want exactly the first 10 digits, if it is not, we'll look at the 10th digit, if that is a comma, we get the first 9 digits, if it is not, we'll look at the 9th and get the first 8 digits and so on...

You can shorten this check if the numbers that make up your strings are always a certain length or less, but this will check each individually

case when right(left(list_of_numbers,11),1)=','
    then left(list_of_numbers,11)
when right(left(list_of_numbers,10),1)=','
    then left(list_of_numbers,10)
when right(left(list_of_numbers,9),1)=','
    then left(list_of_numbers,9)
when right(left(list_of_numbers,8),1)=','
    then left(list_of_numbers,8)
when right(left(list_of_numbers,7),1)=','
    then left(list_of_numbers,7)
when right(left(list_of_numbers,6),1)=','
    then left(list_of_numbers,6)
when right(left(list_of_numbers,5),1)=','
    then left(list_of_numbers,5)
when right(left(list_of_numbers,4),1)=','
    then left(list_of_numbers,4)
when right(left(list_of_numbers,3),1)=','
    then left(list_of_numbers,3)
when right(left(list_of_numbers,2),1)=','
    then left(list_of_numbers,2)
else left(list_of_numbers,1) end as result

the else assumes the string starts with a single digit number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM