Solved

sql statement issue with inserting with a Case statemetn

Posted on 2013-12-11
10
446 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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 500 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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