简体   繁体   English

SAP HANA SQL字符串函数

[英]SAP HANA SQL String functions

I have a table with a column LIST_OF_NUMBERS containing the following strings: 我有一个表,其中的列LIST_OF_NUMBERS包含以下字符串:

10, 20, 395, 443, 534, 734, 954, 105, 156

I want to truncate the LIST_OF_NUMBERS column to 10 characters as follows: 我想将LIST_OF_NUMBERS列截断为10个字符,如下所示:

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. 例如,在我的情况下,我不想显示39,因为它被误解了。 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? 如何在sql脚本中翻译此条件?

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... 它会看第11个字符,如果是逗号,我们需要精确的前10位数字,如果不是,我们将看第10位,如果是逗号,我们将得到前9个数字,如果不是,我们将看第9位并获得前8位,依此类推...

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. 否则,假设字符串以一位数字开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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