Solved

SQL case statement does not work

Posted on 2016-09-21
4
32 Views
Last Modified: 2016-09-21
I have something wrong in below Case Statement.

Msg 1035, Level 15, State 10, Procedure usp_rmcore_invoicesfrt, Line 232
Incorrect syntax near 'Cast', expected 'AS'.

CASE
                   WHEN @AgingBase = 'I' Then Datediff("d", recl.invdate, Cast(CONVERT(VARCHAR(10), Getdate(), 111)))    
                   WHEN @AgingBase = 'D' Then Datediff("d", recl.duedate, Cast(CONVERT(VARCHAR(10), Getdate(), 111)))                    
                   ELSE 0
                   END                           AS AgeDays,
0
Comment
Question by:thayduck
  • 3
4 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 41809304
simple syntax error:

CAST(VALUE AS DataType)


try:
CASE
                   WHEN @AgingBase = 'I' Then Datediff("d", recl.invdate, Cast(CONVERT(VARCHAR(10), Getdate(), 111) AS VARCHAR) )    
                   WHEN @AgingBase = 'D' Then Datediff("d", recl.duedate, Cast(CONVERT(VARCHAR(10), Getdate(), 111) AS VARCHAR) )                   
                   ELSE 0
                   END                           AS AgeDays,

Open in new window


or

CASE
                   WHEN @AgingBase = 'I' Then Datediff("d", recl.invdate, Cast(CONVERT(VARCHAR(10), Getdate(), 111) AS DATETIME2)  )   
                   WHEN @AgingBase = 'D' Then Datediff("d", recl.duedate, Cast(CONVERT(VARCHAR(10), Getdate(), 111) AS DATETIME2) )                   
                   ELSE 0
                   END                           AS AgeDays,

Open in new window

0
 

Author Comment

by:thayduck
ID: 41809350
Both of your answers corrected my problem.

Thank you for the fast response.
0
 

Author Comment

by:thayduck
ID: 41809363
Thanks..
0
 

Author Closing Comment

by:thayduck
ID: 41809373
Thanks....
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question