title: SQL Server : Incorrect syntax
body: <p>I have problem with this T-SQL code:</p>
  
  <pre><code>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)&lt;&gt;'-'))  
            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) &lt; 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)&lt;&gt;'-'))  
            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) &gt; 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)&gt;0
  </code></pre>
  
  <p>When I try run it in DBeaver I get this error:</p>
  
  <blockquote>
    <p><em>SQL Error [156] [S1000]: Incorrect syntax near the keyword 'from'.<br>
    Incorrect syntax near the keyword 'from'.</em></p>
  </blockquote>
  
  <p>My problem is most likely in CASE part, but I can't resolve where exactly. Can you someone help me please?</p>
  
  <p>Sorry for my English. </p>