declare @str nvarchar(250)
set @str=N'汉字转拼音123测试abc'
declare @re nvarchar(max)
declare @i int,@ilen int,@splitchr nvarchar(1)
select @splitchr=' ' --两个拼音之间的分隔符(目的是为了通用性考虑)
,@i=0,@ilen=len(@str),@re=N''
while @i<@ilen
begin
set @i=@i+1
select @re=isnull(rtrim(@re)+@splitchr+PY.拼音+@splitchr , @re+HZ.字符)
from (select substring(@str,@i,1) as 字符) as HZ
left join 汉字拼音对照表 PY on PY.汉字=HZ.字符
end
select (@re) as 拼音
结果:
拼音
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HAN ZI ZHUAN PIN YIN 123 CE SHI abc
(1 行処理されました)