【问题标题】:Formatting string if it's not null如果字符串不为空,则格式化字符串
【发布时间】:2020-10-03 06:33:18
【问题描述】:

根据GMB 对我的previous question 的回答,我简化了查询:

SELECT tn.Date, 
case when tn.DrControl = 7 
    then b1.Name || ' -> ' || c1.Name || ' ' || ifnull(l1.Name, '') || ' ' || ifnull(s1.Name, '') || ' ' || ifnull(p1.Name, '') || ' ' || ifnull(m1.Name , '')
    else b2.Name || ' -> ' || c2.Name || ' ' || ifnull(l2.Name, '') || ' ' || ifnull(s2.Name, '' ) || ' ' || ifnull(p2.Name, '') || ' ' || ifnull(m2.Name, '') 
end Particulars,
case when tn.DrControl = 7 then tn.Amount end DrAmount,
case when tn.CrControl = 7 then tn.Amount end CrAmount,
tn.Narration
FROM Transactions tn
LEFT JOIN Books b1 ON b1.Id = tn.DrBook
LEFT JOIN Books b2 ON b2.Id = tn.CrBook
LEFT JOIN ControlLedgers c1 ON c1.Id = tn.DrControl
LEFT JOIN ControlLedgers c2 ON c2.Id = tn.CrControl
LEFT JOIN Ledgers l1 ON l1.Id = tn.DrLedger
LEFT JOIN Ledgers l2 ON l2.Id = tn.CrLedger
LEFT JOIN SubLedgers s1 ON s1.Id = tn.DrSubLedger
LEFT JOIN SubLedgers s2 ON s2.Id = tn.CrSubLedger
LEFT JOIN Parties p1 ON p1.Id = tn.DrParty
LEFT JOIN Parties p2 ON p2.Id = tn.CrParty
LEFT JOIN Members m1 ON m1.Id = tn.DrMember
LEFT JOIN Members m2 ON m2.Id = tn.CrMember
WHERE 7 IN (tn.DrControl, tn.CrControl)

减少在应用程序代码中创建Particulars 列的开销。它产生这样的结果:

Date        Particulars                             DrAmount     CrAmount     Narration
----------------------------------------------------------------------------------------
2020-06-13  Current Assets -> Cash In Hand Emon     100000       NULL         Some Text

我现在想要的是在Cash In HandEmon 之间放置一个->,例如,如果该列不为空,否则为空字符串。

【问题讨论】:

  • 这不是一个完整的问题,因为它假设任何读者都已经熟悉您之前的问题,但情况可能并非如此。
  • @TimBiegeleisen,我已经为那些不熟悉我之前的问题的人添加了超链接。
  • 将链接中需要的内容直接放入您的问题中。使其自成一体。请在代码问题中提供minimal reproducible example。 (而且这段代码显然不是最小的。)但这显然是一个基本的常见问题解答。请在考虑发布之前阅读您的教科书和/或手册和谷歌任何错误消息或您的问题/问题/目标的许多清晰、简洁和精确的措辞,有和没有您的特定字符串/名称和站点:stackoverflow.com 和标签;阅读许多答案。如果您发布问题,请使用一个短语作为标题。反映你的研究。请参阅How to Ask 和投票箭头鼠标悬停文本。

标签: string sqlite join select sql-null


【解决方案1】:

很遗憾,SQLite 不支持concat_ws(),这使得这样的操作无缝。

一个选项使用case 表达式:

case when tn.DrControl = 7 
then 
    b1.Name 
    || ' -> ' || c1.Name 
    || (case when l1.Name is null then '' else ' -> ' || l1.Name end)
    || (case when s1.Name is null then '' else ' -> ' || s1.Name end )
    || (case when p1.Name is null then '' else ' -> ' || p1.Name end)
    || (case when m1.Name is null then '' else ' -> ' || m1.Name end)
else    
    b2.Name 
    || ' -> ' || c2.Name 
    || (case when l2.Name is null then '' else ' -> ' || l2.Name end)
    || (case when s2.Name is null then '' else ' -> ' || s2.Name end )
    || (case when p2.Name is null then '' else ' -> ' || p2.Name end)
    || (case when m2.Name is null then '' else ' -> ' || m2.Name end)
end Particulars

【讨论】:

    【解决方案2】:

    如果您想用'->' 之类的分隔符连接多个列并且有空列,那么您可以对每个列使用COALESCE(),如下所示:

    coalesce(columnn1, '') || coalesce('->' || column2, '') || coalesce('->' || column3, '') || ....
    

    【讨论】:

    • 是否可以将您和GMB 的答案都设置为接受的答案?
    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多