Solved

SQL case statement does not work

Posted on 2016-09-21
4
27 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backups - Strategies 1 21
affinity mask in sql server 1 22
select over clause 1 40
SQL server is using more virtual memory. 5 67
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now