因为我很无聊。我几乎肯定这在应用程序中没有用,但它确实可以满足您的要求。如果你真的想要,你可以用这个创建一个函数......
Declare @Nvarchar Nvarchar(25) = N'Hɶppy',
@NVbinary Varchar(128),
@parse Int = 3,
@NVunit Varchar(4),
@result Varchar(64) = '0x',
@SQL Nvarchar(Max);
Select @NVbinary = master.sys.fn_varbintohexstr(Convert(Varbinary(128),@Nvarchar))
While (@parse < Len(@NVbinary))
Begin
Select @NVunit = Substring(@NVbinary,@parse,4),
@parse = @parse + 4
If Substring(@NVunit,3,2) = '00'
Begin
Set @result = @result + Substring(@NVunit,1,2)
End
Else
Begin
Set @result = @result + '22' -- 22 is the hex value for quotation mark; Use Select Convert(Varbinary(8),'"') to get the value for whatever non-unicode character you want.
End
End
Set @SQL = 'Select Convert(Varchar(128),' + @result + '), ''' + @result + ''''
Select @Nvarchar, @NVbinary
Exec sp_executeSQL @SQL