?
Solved

Conditional value copying in columns

Posted on 2005-04-27
2
Medium Priority
?
274 Views
Last Modified: 2010-03-19
Hi,

I've created a temp table where one of the columns (Col. A) has a positive or negative value. I want to copy all the positive values in (Col. B) and the negative values in (Col. C) in the same temp table.

For example
#temp1
A        B        C
2        2
3        3
-4                -4
6        6
-7                -7
-8               -8


How will I update columns B and C.
say,
Update #temp1
Case
     when #temp1.A is positive  then set #temp1.B = #temp1.A
      else
          set #temp1.C = #temp1.A
end

This is in plain english.. how would you write this in a code, saying for the positive or negative numbers?

Many thanks

SKM



0
Comment
Question by:SKM061104
2 Comments
 
LVL 28

Accepted Solution

by:
rafrancisco earned 375 total points
ID: 13877119
UPDATE #Temp1
SET ColB = CASE WHEN ColA > 0 THEN ColA ELSE NULL END,
      ColC = CASE WHEN ColA < 0 THEN ColA ELSE NULL END
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13877309
You don't really even need other physical columns; you can use computed/virtual columns that SQL will generate at run time rather than storing:


ALTER TABLE #Temp1
ADD B AS CASE WHEN A >= 0 THEN A ELSE NULL END,
    C AS CASE WHEN A < 0 THEN A ELSE NULL END
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

850 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