基于否定更新一列的查询 [英] Update query on one column based on negative

查看:48
本文介绍了基于否定更新一列的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要根据A列更新B列,

条件 - 如果A列(数据类型 - Float)为负,则将-1放入B列,反之亦然。 />


使用SQL Server 2014



我尝试过:



如果更新查询但未能从A列中提取否定的情况



来自解决方案的OP代码:

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:

update v_table1 set ColumnB= case when ColumnA <0 then -1 when ColumnA >0 then 1 when ColumnA =0 then 0 else null end

推荐答案

很难从
Quote:

条件 - 如果列A(数据类型 - 浮点数)为负,则输入-1登录列B,反之亦然

Condition - If Column A(data type-Float) is Negative then Put -1 Sign in Column B and vice versa

您已经拥有的代码是放置ColumnB中的似乎有效,但你已经提到了符号。



所以如果ColumnB是char类型那么您似乎想要

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

update v_table1 set ColumnB= case when Isnull(ColumnA,0) <0 then '-' when Isnull(ColumnA,0) >0 then '1' else '0' end

注意我拿出了你的null。



但是,如果你真正想要的是根据ColumnA的值否定ColumnB,那么你想要这样的东西

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

update v_table1 set ColumnB= case when Isnull(ColumnA,0) <0 then -1 * ColumnB when Isnull(ColumnA,0) >0 then ColumnB else 0 end

请注意,这仅在ColumnB尚未为负数时才有效。



如果这不是您要求的那么请使用有问题或评论吗?此解决方案旁边的链接我将尝试进一步帮助

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


更新v_table1设置ColumnB = ColumnA时的情况< 0然后当ColumnA> 0时为-1然后当ColumnA = 0时为1然后0 else null end
update v_table1 set ColumnB= case when ColumnA <0 then -1 when ColumnA >0 then 1 when ColumnA =0 then 0 else null end


这篇关于基于否定更新一列的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆