简体   繁体   English

替换 nvarchar(50) 列中的值

[英]Replacing values in a nvarchar(50) column

I have a column in my SQL Server table that is a nvarchar(50) .我的 SQL 服务器表中有一个列是nvarchar(50) I'm looking for a way to replace values in the column.我正在寻找一种方法来替换列中的值。

So for example, This is my column with the original values:例如,这是我的原始值列:

随附的

What I want is for each value to equal the following value:我想要的是每个值等于以下值:

请查看第二张图片以查看所需的输出

I can get this working with a replace function, however it then moves it to a new column for each value I'm replacing.我可以通过替换 function 来解决这个问题,但是它会将它移动到我要替换的每个值的新列中。 Ideally what I would like is to have a single column with my new changes.理想情况下,我希望在一个专栏中包含我的新更改。

I cannot use the update function as I only have read access to this table.我不能使用更新 function 因为我只有对该表的读取权限。

Any info would be great.任何信息都会很棒。

Thanks.谢谢。

Sample data样本数据

create table MyTable
(
  Task nvarchar(50)
);

insert into MyTable (Task) values
('VLV LOADING/RELEASE 1st DS'),
('VLV LOADING/RELEASE 2nd DS'),
('VLV LOADING/RELEASE 3rd DS'),
('VLV LOADING/RELEASE 1st DS'),
('VLV LOADING/RELEASE 2nd DS'),
('VLV LOADING/RELEASE 3rd DS');

Solution解决方案

Option 1选项1

Using a case expression.使用case表达式。

select case mt.Task
         when 'VLV LOADING/RELEASE 1st DS' then 'Proximal Release Force-S'
         when 'VLV LOADING/RELEASE 2nd DS' then 'Proximal Release Force-L'
         when 'VLV LOADING/RELEASE 3rd DS' then 'Proximal Release Force-M'
       end as Task
from MyTable mt;

Option 2选项 2

In case the replacement values are available in another table, then use a join .如果替换值在另一个表中可用,则使用join

create table Task
(
  Id int,
  Task nvarchar(50),
  Description nvarchar(50)
);

insert into Task (Id, Task, Description) values
(10, 'VLV LOADING/RELEASE 1st DS', 'Proximal Release Force-S'),
(11, 'VLV LOADING/RELEASE 2nd DS', 'Proximal Release Force-M'),
(12, 'VLV LOADING/RELEASE 3rd DS', 'Proximal Release Force-L');

select t.Description as Task
from MyTable mt
join Task t
  on t.Task = mt.Task;

Result结果

Task
------------------------
Proximal Release Force-S
Proximal Release Force-M
Proximal Release Force-L
Proximal Release Force-S
Proximal Release Force-M
Proximal Release Force-L

Fiddle to see things in action. 小提琴以查看实际情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM