如何在SQL选择查询中添加大小写表达式

时间:2019-06-05 04:54:51

标签: sql sql-server case

我尝试了以下查询来获取客户的收款明细。在此案例陈述中出了什么问题?我需要的是显示付款类型(如果有金额)。

select convert(varchar,a.DocDate,110) as DocDate,
       a.CardCode,
       a.CardName,
       d.SlpName,
       e.CheckNum,
       (case when a.CashSum >= 0 then "Cash Payment" when a.CreditSum >= 0 then "Credit Card Payment" when a.CheckSum >= 0 then "Cheque Payment" else "Bank Transfer" end) as 'Payment type',
       (ISNULL(a.CashSum,0) + ISNULL(a.CreditSum,0) + ISNULL(a.CheckSum,0) + ISNULL(a.TrsfrSum,0)) as DocTotal 
from ORCT a
left join RCT2 b on b.DocNum=a.DocNum
Left Join OINV c on c.DocNum = b.DocEntry
Left join OSLP d on d.SlpCode = c.SlpCode
left join RCT1 e on e.DocNum = a.DocEntry

它给出以下错误 enter image description here

2 个答案:

答案 0 :(得分:3)

在SQL Server中,文字字符串括在单引号'中,而不括在双引号"中。

另一方面,列(对象)名称包含在括号[Payment type]或双引号"Payment type"中。

因此,您的查询应类似于:

case 
    when a.CashSum >= 0 then 'Cash Payment' 
    when a.CreditSum >= 0 then 'Credit Card Payment'
    when a.CheckSum >= 0 then 'Cheque Payment'
    else 'Bank Transfer' 
end as [Payment type]

答案 1 :(得分:1)

THEN块中的以下case表达式中,将双引号替换为单引号:

(case when a.CashSum >= 0 then "Cash Payment" when a.CreditSum >= 0 then "Credit Card Payment" when a.CheckSum >= 0 then "Cheque Payment" else "Bank Transfer" end) as 'Payment type',

(case when a.CashSum >= 0 then 'Cash Payment' when a.CreditSum >= 0 then 'Credit Card Payment' when a.CheckSum >= 0 then 'Cheque Payment' else 'Bank Transfer' end) as 'Payment type',

db <>小提琴演示的示例:

THEN中使用dobule quotes时收到错误

THEN中更改为single quotes后的工作