简体   繁体   English

Oracle-案例说明

[英]Oracle - Case Statement

Hello – I am seeking your help with the below case statement but I am not sure how to change it accordingly. 您好–我在以下案例声明中寻求您的帮助,但是我不确定如何相应地进行更改。 Could you please help? 能否请你帮忙? New to SQL but learning. SQL新手,但学习。

This is what is required 这是必需的

When sales_number start with zero/s then remove leading zeros so the sales_number is a total of 14 characters Then concatenate sales_number (14 digits) and sales_date and sales_amount When the sales_number is less than 16 characters Then concatenate the last nine characters from sales_number and sales_date and sales_amount 当sales_number以零/ s开头,然后删除前导零,因此sales_number总共为14个字符,然后将sales_number(14位数字)与sales_date和sales_amount连接起来,当sales_number小于16个字符时,然后将sales_number和sales_date和销售额

This is my attempt to create the case statement 这是我创建案例陈述的尝试

CASE
when length(to_char(remove leading zeros from sales_number)) = 14
then to_char(sales_number without leading zeros)) || sales_date ||sales_amount
when  sales_number < 16
then right(to_char(last nine characters from sales_number and sales_date and sales_amount)

Many Thanks. 非常感谢。

Maybe this can give on idea on what you need; 也许这可以让您对您的需求有所了解;

with test(sales_number, sales_date, sales_amount) as 
(
select '001245JNF690864350', sysdate, 100 from dual union all
select 'SD895394KK9746K0000334', sysdate, 100 from dual union all
select '3850956789025417', sysdate, 100 from dual
)
select case
        when regexp_like(sales_number, '^0{1,}') /* starts with one or more zeros */ 
            then ltrim(sales_number, '0') /* remove leading zeros */
                 || sales_date ||sales_amount
        when length(sales_number) = 16
            then substr(sales_number, -9) /* last 9 chars */
                 || sales_date ||sales_amount
        else
            '' /* what to do here? */ 
    end
from test

It's not that clear to me what to do when none of your conditions is matched, so i left it blank; 当您的条件都不匹配时,我不清楚该怎么办,所以我把它留空了。 hope this can help to find your solution 希望这可以帮助您找到解决方案

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

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