Solved

SQL case statement does not work

Posted on 2016-09-21
4
37 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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