SQL Server 2008 - 添加到名称列中特定位置的字符串 [英] SQL Server 2008 - Add to string in particular position within name column

查看:43
本文介绍了SQL Server 2008 - 添加到名称列中特定位置的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求做的工作有点超出了我的 SQL 技能,并且在网上做了一些研究,找不到在 SQL Server 2008 而不是 MySQL 中完成的写入解决方案.

I have been asked to do a job which is beyond my SQL skills a little and having done some research online, cannot quite find the write solution to be done in SQL Server 2008 and not MySQL.

我有一个表格,我需要通过在某个点之后添加一个额外的字符串来更新特定名称,同时保持右边的其余字符串完好无损.

I have a table where I need to update specific names by adding an additional string after a certain point whilst keeping the rest of the string to the right intact.

e.g.

Current Name = 'Name - Location - 0005'
New Name = 'Name (West) - Location - 0005'

如您所见,我需要添加文本(West),我有一个列出所有代码(例如 0005)的表格,我需要将其链接到我的 where 子句中以仅更新相关名称.

As you can see I need to add the text (West), I have a table which lists all the codes (e.g. 0005) and I need to link that into my where clause to only update the relevant names.

所以问题是:

1 - 如何通过在设置位置(第 5 个字符)添加附加文本来更新名称,同时保留名称右侧的任何文本

1 - How can I update the Name by adding additional text at a set location (5th character), whilst maintaining whatever text is to the right of the names

2 - 有没有一种方法可以让我检查代码,我尝试使用包含但是该表没有全文索引.这不是一个大问题,因为我可以手动为表中的每一行创建更新语句,很高兴知道添加到我的知识库中.

2 - Is there a way that I can do a sort of like in to check the code, I tried using Contains however the table is not full-text indexed. This is not a massive issue as I can manually create update statement using just like for each row within the table, would just be a nice to know to add to my knowledge base.

推荐答案

STUFF的帮助下,我们可以实现这一点

with the help of STUFF we can achieve this

STUFF ( character_expression , start , length , replaceWith_expression )
SELECT STUFF('Name - Location - 0005', 5, 0, ' (West)');

输出将是 Name (West) - Location - 0005

这篇关于SQL Server 2008 - 添加到名称列中特定位置的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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