Sql server中有没有replace语句? 没有的话,怎样实现类似于mySql中的repla

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql server中有没有replace语句? 没有的话,怎样实现类似于mySql中的repla相关的知识,希望对你有一定的参考价值。

Sql server中有没有replace语句?
没有的话,怎样实现类似于mysql中的replace功能(有重复的记录就更新数据,没有重复记录就插入数据)?

sqlserver中有replace语句。

工具:sqlserver 2008R2

步骤:

1、如学生表中有如下数据:

2、现要将表中的学号中的0都显示成a,可用如下语句:

select replace(学号,\'0\',\'a\') 学号,姓名,性别,年龄 from 学生

3、可以看到学号部分的0都变成了a,如图:

replace函数使用语法:

replace(s1,s2,[s3]) 即s3替换s1中的s2字符,s3可以不填,默认是空字符。

参考技术A /*
MySQL
replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中
1、如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。
2、 否则,直接插入新数据。
MSSQL(2008+)
判断t_B记录在T_A就更新,否则就插入
*/
Merge Into t_A As a 
Using t_B as b  on a.id=b.id 
When Matched then 
    update set a.SNO=b.SNO 
When Not Matched then 
    Insert(id,SNO) values(b.id,b.SNO);

追问

谢谢!能结合一个例子,给个等价的sql 语句吗?

追答MySQL的
Replace into table(id, update_time) values(1, now())
MSSQL的
Merge Into table As a 
Using (select 1 As id,getdate() As update_time) as b  on a.id=b.id 
When Matched then 
    update set a.update_time=b.update_time
When Not Matched then 
    Insert(id,update_time) values(b.id,b.update_time);
    
MySQL的
Replace into Table(id, update_time) 
    Select id,update_time From Other
MSSQL的
Merge Into Table As a 
Using (Select id,update_time From Other) as b  on a.id=b.id 
When Matched then 
    update set a.update_time=b.update_time
When Not Matched then 
    Insert(id,update_time) values(b.id,b.update_time);

数据库SQL server中 replace使用方法

replace使用方法:
REPLACE
(
''string_replace1''
,
''string_replace2''
,
''string_replace3''
)
参数
''string_replace1''
待搜索的字符串表达式。string_replace1
可以是字符数据或二进制数据。
''string_replace2''
待查找的字符串表达式。string_replace2
可以是字符数据或二进制数据。
''string_replace3''
替换用的字符串表达式。string_replace3
可以是字符数据或二进制数据。
返回类型为字符串
参考技术A 不太清楚你要删除多少个那种符号,如果能全部列出来,倒是有一个比较笨的方法。
嵌套使用Replace.
Select
Replace(Replace(列名,'ɦ',‘’),‘&&621’,'')本回答被提问者采纳

以上是关于Sql server中有没有replace语句? 没有的话,怎样实现类似于mySql中的repla的主要内容,如果未能解决你的问题,请参考以下文章

数据库SQL server中 replace使用方法

数据库SQL server中 replace使用方法

sql server replace 函数使用方法

sql server 里类似replace的字符串子串删除

sql server2005中 如何把带逗号的数字字符串转换为数字。除了replace,要函数转换的方法

sql 删除指定字符串