Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need to Update a column B on basis on Column A,
Condition - If Column A(data type-Float) is Negative then Put -1 Sign in Column B and vice versa.

using SQL Server 2014

What I have tried:

Case if update query but failed to extract negative from column A

OP Code from solution:
SQL
update v_table1 set ColumnB= case when ColumnA <0 then -1 when ColumnA >0 then 1 when ColumnA =0 then 0 else null end
Posted
Updated 16-Jul-18 22:04pm
v2
Comments
Animesh Datta 17-Jul-18 2:57am    
have you tried something ?
put your effort
Member 13518187 17-Jul-18 3:02am    
Not getting that's why I came here to ask
Patrice T 17-Jul-18 3:05am    
And you some code ?

It's difficult to understand exactly what you are trying to do from
Quote:
Condition - If Column A(data type-Float) is Negative then Put -1 Sign in Column B and vice versa
The code you already have is putting a value in ColumnB and appears to work, but you have referred to a "sign".

So if ColumnB is a char type then you appear to want
SQL
update v_table1 set ColumnB= case when Isnull(ColumnA,0) <0 then '-' when Isnull(ColumnA,0) >0 then '1' else '0' end
Note I took out your null.

But, if what you really want is to negate ColumnB based on the value of ColumnA then you want something like this
SQL
update v_table1 set ColumnB= case when Isnull(ColumnA,0) <0 then -1 * ColumnB when Isnull(ColumnA,0) >0 then ColumnB else 0 end
Note this will only work if ColumnB is not already negative.

If this is not quite what you were asking then use the "Have a Question or Comment?" link next to this solution and I will try to help further
 
Share this answer
 
update v_table1 set ColumnB= case when ColumnA <0 then -1 when ColumnA >0 then 1 when ColumnA =0 then 0 else null end
 
Share this answer
 
Comments
CHill60 17-Jul-18 3:53am    
By posting your code as a solution you have removed your question from the list of unanswered posts. There is an "Improve Question" link next to your question that you can use to update it with information like this. I will do it for you this time, but I suggest you delete this solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900