Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 替换列值_Sql_Sql Server - Fatal编程技术网

Sql 替换列值

Sql 替换列值,sql,sql-server,Sql,Sql Server,我有这张桌子 Name Rating A 2 B 1 C 5 D 3 E 1 F 4 我有一个评级系统 1-Excellent, 2-Very Good, 3-Good, 4-OK, 5-Poor 我想知道是否可以替换表中的nueric值以得到下面的结果表 Name Rating A Very Good B

我有这张桌子

Name       Rating
A            2
B            1 
C            5
D            3
E            1
F            4
我有一个评级系统

1-Excellent, 2-Very Good, 3-Good, 4-OK, 5-Poor
我想知道是否可以替换表中的nueric值以得到下面的结果表

Name       Rating
A         Very Good
B         Excellent
C           Poor
D           Good
E         Excellent
F            OK

谢谢

我认为就地更新数据不是个好主意,最好存储评级id,而不是数据的文本表示。但您可以查询表并用文本替换int:

select
    Name,
    case Rating
        when 1 then 'Excellent'
        when 2 then 'Very Good,'
        when 3 then 'Good'
        when 4 then 'OK'
        when 5 then 'Poor'
    end as Rating
from <your table>
或者您可以创建一个查找表并与之联接

create table Rating (id int, desc nvarchar(128))

insert into Rating (id, desc)
select 1, 'Excellent' union all
select 2, 'Very good' union all
select 3, 'Good' union all
select 4, 'OK' union all
select 5, 'Poor'

select
    t.Name,
    R.desc as Rating
from <your table> as t
    left outer join Rating as R on R.id = t.Rating

您可以使用更新功能:

update yourtable set rating = 'Excellent' where rating = '1'
邹可以对你所有的评分进行更新

使用案例陈述。当然,这仅在列未设置为数值时有效

UPDATE tblRatings
SET Rating = CASE WHEN 1 THEN 'Excellent'
                  WHEN 2 THEN 'Very Good'
                  WHEN 3 THEN 'Good'
                  WHEN 4 THEN 'OK'
                  ELSE 'Poor' 
             END
如果是,则需要使用SELECT语句

SELECT CASE WHEN 1 THEN 'Excellent'
            WHEN 2 THEN 'Very Good'
            WHEN 3 THEN 'Good'
            WHEN 4 THEN 'OK'
            ELSE 'Poor' 
       END
  FROM tblRatings
试用

如果sql server 2012

像这样:

 select Rating,name,choose(Rating,'Excellent','Very Good','Good','OK','Excellent','Poor') from table

“为什么你有两次优秀的表现?”sidux他编辑了这个问题。你的评论不再有意义了。好吧,我看不到任何标签!但是是的,我应该提到sql server 2012,谢谢!他写道,评级是数字!你的答案行不通!他写道,评级是数字!你的答案行不通!
 select Rating,name,choose(Rating,'Excellent','Very Good','Good','OK','Excellent','Poor') from table