您好 - 我正在寻求您对以下案例陈述的帮助,但我不确定如何相应更改。能否请你帮忙? SQL新手,但学习。
这是必需的
当sales_number以零/秒开头时,删除前导零,使sales_number总共14个字符 然后连接sales_number(14位)和sales_date以及sales_amount 当sales_number少于16个字符时 然后连接sales_number和sales_date以及sales_amount
中的最后九个字符这是我尝试创建案例陈述
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)
非常感谢。
答案 0 :(得分:1)
也许这可以给出你需要的想法;
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
当你的条件没有匹配时,我不清楚该做什么,所以我把它留空;希望这有助于找到您的解决方案