T-SQL syntax question

Hi

I have a table with 4 fields, the last field is an amount, which can be positive or negative. I want to insert the data from that table in a table that has the same first 3 fields and then 2 amount fields, one for positive, one for negative amounts.

I am trying to use the "Insert Into <new table> (f1, f2, f3, apos, aneg) select f1, f2, f3, a1, a1 from <old table>" syntax.
But I don't know how to specify the fields so that:
- if a1 is positive, then apos = a1, aneg=0
- if a1 is negative, then apos = 0, aneg=a1

Can someone help me with how to achieve what I need ?

Thanks
Bernard
LVL 1
bthouinIT Analyst and developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Well, you do it exactly like you wrote:

INSERT INTO <new table> (f1, f2, f3, apos, aneg)
SELECT  f1, f2, f3, IIF(a1>0, a1, 0), IIF(a1>0, 0, a1)
FROM    <old table>;

Open in new window


Before SQL Server 2012:

INSERT INTO <new table> (f1, f2, f3, apos, aneg)
SELECT  f1, f2, f3, CASE WHEN a1>0 THEN a1 ELSE 0 END, CASE WHEN a1>0 THEN 0 ELSE a1 END
FROM    <old table>;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
A pair of CASE statements with logic to pick off positives and negatives will work.

<air code, modify to meet your needs>
Insert Into <new table> (f1, f2, f3, apos, aneg) 
SELECT blah, yadda, yabba, 
   CASE WHEN a1 > 0 THEN a1 ELSE 0 END as postive_column, 
   CASE WHEN a1 < 0 THEN a1 ELSE 0 END as negative_column
FROM old_table

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:

Insert Into newtable (f1, f2, f3, apos, aneg)
select f1, f2, f3,
case when  a1 >=0 then a1 else 0 end,
case when  a1 >=0 then 0 else a1 end
from oldtable
bthouinIT Analyst and developerAuthor Commented:
Hi all of you

Thanks for the solutions, I have used the CASE solution first suggested by ste5an, so he gets the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
T-SQL

From novice to tech pro — start learning today.