1

我对这个 T-SQL 代码有疑问:

SELECT 
  DQ.Code as DQ_Code, II.OrdNumber AS Cislo, II.Id As ID,
  convert(varchar(10), cast(II.DocDate$DATE as datetime), 104) as DatumPlneni,
  convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) as DatumSplatnosti,
  F.Name as Firma, F.ID as FIRMID,
(II.LocalAmount - II.LocalCreditAmount) as LocalAmount,
(II.LocalPaidAmount) as LocalPaidAmount,
(II.LocalAmount - II.LocalCreditAmount - II.LocalPaidAmount + II.LocalPaidCreditAmount) as LocalNotPaidAmount,
 case
   when 
    ((case 
        when len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=10 
          then convert(varchar(10), cast(II.DueDate$DATE as datetime), 104)
        when ((len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=9) and (substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 7 for 1) ='-'))
          then substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 1 for 5)||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 6 for 10)  
        when ((len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=9) and (substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 7 for 1)<>'-'))  
          then substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 1 for 8)||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 9 for 10)  
        when len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=8 
          then substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 1 for 5)||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 6 for 2) ||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 8 for 2) 
      end) < cast(current_date as varchar(10)) 
     )
    then II.LocalAmount - II.LocalCreditAmount - II.LocalPaidAmount + II.LocalPaidCreditAmount
   when 
    ((case 
        when len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=10 
          then convert(varchar(10), cast(II.DueDate$DATE as datetime), 104)
        when ((len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=9) and (substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 7 for 1) ='-'))
          then substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 1 for 5)||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 6 for 10)  
        when ((len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=9) and (substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 7 for 1)<>'-'))  
          then substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 1 for 8)||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 9 for 10)  
        when len(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) )=8 
          then substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 1 for 5)||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 6 for 2) ||'0'||substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 8 for 2) 
      end) > cast(current_date as varchar(10)) 
     )
    then 0
    end as NotPaidAfterDueD
FROM 
  IssuedInvoices II
LEFT JOIN DocQueues DQ ON DQ.ID=II.Docqueue_ID
LEFT JOIN Firms F ON F.ID=II.Firm_ID
WHERE 
  (II.LocalAmount - II.LocalCreditAmount - II.LocalPaidAmount + II.LocalPaidCreditAmount)>0

当我尝试在 DBeaver 中运行它时,我收到此错误:

SQL 错误 [156] [S1000]:关键字 'from' 附近的语法不正确。
关键字“from”附近的语法不正确。

我的问题很可能出现在 CASE 部分,但我无法解决具体在哪里。你能帮我吗?

对不起我的英语不好。

4

1 回答 1

11

你有几个问题。

首先,在您的CASE语句中,您使用的语法substring类似于:

substring(convert(varchar(10), cast(II.DueDate$DATE as datetime), 104) from 7 for 1) ='-')

substring()语法是:

SUBSTRING ( expression ,start , length )

你会注意到没有from使用。所以你的代码会是这样的:

substring(convert(varchar(10), cast(II.[DueDate$DATE] as datetime), 104), 7, 1) ='-'

其次,您在 SQL Server 中使用管道||将数据连接在一起,您需要使用加号+。所以替换:

...5)||'0'||substring...

和:

...5)+'0'+substring...

第三,将 替换current_dategetdate()current_date替换为时您必须解决的另一件事getdate()是:

cast(getdate() as varchar(10))

这将以您不想使用的格式返回当前日期的值。我建议您查看以下网站,以确定您比较的最佳格式:

如何在 Sql Server 2005 中格式化日期时间和日期

通常,您会想要使用类似的东西:

convert(varchar(8), getdate(), 112) – yyyymmdd

最后,任何包含特殊字符的列都DueDate$DATE需要用方括号括起来。所以它们看起来像:

...cast(II.[DueDate$DATE]...
于 2013-02-22T19:08:05.720 回答