Last active
January 25, 2025 15:40
-
-
Save sevaa/f084a0a5a994c3bc28e518d5c708d5f6 to your computer and use it in GitHub Desktop.
Converting an NVARCHAR string to a UTF-8 VARBINARY data block in pure Transact-SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function [dbo].[ToUTF8](@s nvarchar(max)) | |
returns varbinary(max) | |
as | |
begin | |
declare @i int = 1, @n int = datalength(@s)/2, @r varbinary(max) = 0x, @c int, @c2 int, @d varbinary(4) | |
while @i <= @n | |
begin | |
set @c = unicode(substring(@s, @i, 1)) | |
if (@c & 0xFC00) = 0xD800 | |
begin | |
set @i += 1 | |
if @i > @n | |
return cast(cast('Malformed UTF-16 - two nchar sequence cut short' as int) as varbinary) | |
set @c2 = unicode(substring(@s, @i, 1)) | |
if (@c2 & 0xFC00) <> 0xDC00 | |
return cast(cast('Malformed UTF-16 - continuation missing in a two nchar sequence' as int) as varbinary) | |
set @c = (((@c & 0x3FF) * 0x400) | (@c2 & 0x3FF)) + 0x10000 | |
end | |
if @c < 0x80 | |
set @d = cast(@c as binary(1)) | |
if @c >= 0x80 and @c < 0x800 | |
set @d = cast(((@c * 4) & 0xFF00) | (@c & 0x3F) | 0xC080 as binary(2)) | |
if @c >= 0x800 and @c < 0x10000 | |
set @d = cast(((@c * 0x10) & 0xFF0000) | ((@c * 4) & 0x3F00) | (@c & 0x3F) | 0xe08080 as binary(3)) | |
if @c >= 0x10000 | |
set @d = cast(((@c * 0x40) & 0xFF000000) | ((@c * 0x10) & 0x3F0000) | ((@c * 4) & 0x3F00) | (@c & 0x3F) | 0xf0808080 as binary(4)) | |
set @r += @d | |
set @i += 1 | |
end | |
return @r | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, it works perfectly, also for chinese chars, and other special chars.
I have to prepare data on a SQL 2016 to send it to the Salesforce API as BASE64.
When I used CAST(MyText AS VARBINARY(MAX)) AS MyText_VARBINARY, and then encoded that to BASE64 with CAST('' AS XML).value('xs:base64Binary(sql:column("MyText_VARBINARY"))', 'VARCHAR(MAX)'), I got a BASE64 which had UTF16-LE encoding.
With your function ToUTF8, I got the corrct encoding!