?
Solved

sql statement issue with inserting with a Case statemetn

Posted on 2013-12-11
10
Medium Priority
?
448 Views
Last Modified: 2013-12-11
Hi,
I'm having an issue with an insert statement.
I want to copy some records from one table to the other

when field "Action_name" is equal to credit I want to insert the negative of the amount. So if it says credit and the amount is 2, i want to insert -2.

I am using the following statement to do that, somehow it is giving me the message, incorrect syntax near '('

the code is as follows

insert into  [dbo].[tbl_visa_imports](
      
      [cc] ,
      [exp] ,
      [media_name] ,
      [trans_date] ,
      [transid] ,
      [ref_transid] ,
      (case
      when action_name = 'credit' then abs(([amount])*-1)
      else [amount] end) ,
      [action_name] ,
      [status_name] ,
      [name] ,
      [ticket],
      
      [billingid],
      [custid],
      [fail_name],

      [operator],
      [entry_mode] ,
      [USERID] ,
      [CASHDRAWERID] ,
      [DEPTABBR] ,
      [DEPTEXTNAME] ,


      [LOCABBR] ,
      [LOCID] ,
      [LOCNAME] ,
      [PATDAT] ,
      [PATID] ,
 
      [SERVICEARBAABBR] ,
      [SERVICEAREAID] ,
      [SERVICEAREANAME] ,

      [GUARACCTID] ,
      [HOSPACCTID] ,
      [WORKSTATIONID],
      [WORKSTATIONNAME],
      assigned )
      
      select       [cc] ,
      [exp] ,
      [media_name] ,
      [trans_date] ,
      [transid] ,
      [ref_transid] ,
      [amount] ,
      [action_name] ,
      [status_name] ,
      [name] ,
      [ticket],
      
      [billingid],
      [custid],
      [fail_name],

      [operator],
      [entry_mode] ,
      [USERID] ,
      [CASHDRAWERID] ,
      [DEPTABBR] ,
      [DEPTEXTNAME] ,
      
      [LOCABBR] ,
      [LOCID] ,
      [LOCNAME] ,
      [PATDAT] ,
      [PATID] ,

      [SERVICEARBAABBR] ,
      [SERVICEAREAID] ,
      [SERVICEAREANAME] ,

      [GUARACCTID] ,
      [HOSPACCTID] ,
      [WORKSTATIONID],
      [WORKSTATIONNAME],
      0
      from dbo.tbl_imports

thanks in advance
Vinnie

Addition: my main problem is in the Case Statement. If I was to take out the case statement, it works fine

   (case 
      when action_name = 'credit' then abs(([amount])*-1)
      else [amount] end) ,

Open in new window

0
Comment
Question by:damixa
[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
  • 3
  • 2
  • +2
10 Comments
 

Author Comment

by:damixa
ID: 39712699
So to clarify, my main problem is in the case statement

   (case 
      when action_name = 'credit' then abs(([amount])*-1)
      else [amount] end) ,

Open in new window

0
 
LVL 1

Expert Comment

by:jleenotlo
ID: 39712737
Take out the abs() because that would give you a positive number instead of a negative number.

(case
      when action_name = 'credit' then ([amount]*-1)
      else [amount] end)
0
 

Author Comment

by:damixa
ID: 39712743
Right but I want a positive number. That way the outcome is always negative. So by adding the abb and multiplying it by neg 1 it assures me that the outcome is always the negative number.

I did try that though, and it still gave me the same error.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 39712749
>I am using the following statement to do that, somehow it is giving me the message, incorrect syntax near '('

The CASE statement is currently in the INSERT clause, which is causing the syntax error.  It belongs in the SELECT clause that lines up with a column where that CASE block is to insert values.
0
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 2000 total points
ID: 39712753
The Case statement is in the wrong part. The insert statement must have the field name you are inserting to and the case statement should be in the select statement.


Kelvin
0
 

Author Comment

by:damixa
ID: 39712761
Kelvin, I'm so stupid. Thanks
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39712763
>when field "Action_name" is equal to credit I want to insert the negative of the amount. So if it says credit and the amount is 2, i want to insert -2.

Replace column_name with the name of the column that you're trying to insert the expression into, guessing it's amount.
INSERT INTO tbl_visa_imports (blah, blah, column_name)
SELECT blah, blah, 
 CASE WHEN Action_Name = credit then amount * -1 ELSE amount END
FROM Imports

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39712770
damixa - Let me know if my first comment did not meet your needs.
0
 
LVL 12

Expert Comment

by:duttcom
ID: 39712777
You can put the case statement in the select statement rather than the insert.

Your insert statement would only have [amount] rather than the case statement and the case statement would be in the select statement like so-

[ref_transid] ,
CASE WHEN action_name = 'credit'
then abs(([amount])*-1)
else [amount]
END,
 [action_name]

The whole case statement is the value being selected to insert.
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 39712778
No problems - only recognised it as I'd spent a few hours a short while ago finding the exact same issue in my own code.

Glad I could help.


Kelvin
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

762 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