[英]Any way to change specific data in a column e.g Mr to Dr which contains a name
I am trying to change the Titles of 'doctors' in a database and was just wondering was there a SQL query which I could run to change them. 我试图更改数据库中“医生”的头衔,只是想知道是否存在可以用来更改它们的SQL查询。
The column im trying to change 我试图改变的专栏
What I am asking is that there is any way I can update the column to add a 'Dr' infront of the names to replace the 'Miss','Mr' etc. 我要问的是,有什么方法可以更新该列,以便在名称的前面添加一个“ Dr”以替换“ Miss”,“ Mr”等。
I'm thinking about using a SQL Statement containing the wildcard function to update it but not sure it would change the specifics. 我正在考虑使用包含通配符函数的SQL语句来更新它,但不确定是否会更改细节。
Thanks, 谢谢,
Karl 卡尔
Use REPLACE
option 使用
REPLACE
选项
UPDATE table_name
SET column_name = REPLACE(column_name, 'Mr.', 'Dr.')
WHERE column_name LIKE 'Mr.%'
try this 尝试这个
Update myTable
set name = replace(replace(name,'Miss ','Dr '),'Mr ','Dr ')
I might suggest doing: 我可能建议您这样做:
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr');
This only replaces the first word in the string. 这只会替换字符串中的第一个单词。 You might want to be extra careful and add a
where
clause. 您可能需要格外小心,并添加一个
where
子句。
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr')
where col like 'Miss %' or col like 'Mr %' or
col like 'Mrs %' or col like 'Ms %';
The problem with replace()
is that it replaces all occurrences in the string. replace()
的问题在于它将替换字符串中的所有匹配项。 Although the honorifics are unlikely to be in a name, you could have names like "Missy Elliott". 尽管称呼不太可能使用名称,但是您可以使用“ Missy Elliott”之类的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.