Solved

sql statement issue with inserting with a Case statemetn

Posted on 2013-12-11
10
442 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
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 65

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 65

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 65

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

10 Experts available now in Live!

Get 1:1 Help Now