Solved

SQL case statement does not work

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i use WITH CTE for checking exist value? 3 55
SQL Insert parts by customer 12 51
Index and Stats Management-Specific tables 8 38
MS SQL Conditional WHERE clause 3 36
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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