Home
%3CLINGO-SUB%20id%3D%22lingo-sub-737053%22%20slang%3D%22en-US%22%3ERe%3A%20Introducing%20UTF-8%20support%20for%20SQL%20Server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-737053%22%20slang%3D%22en-US%22%3E%3CP%3EA%20nice%20clear%20and%20helpful%20article.%20Thanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-747705%22%20slang%3D%22en-US%22%3ERe%3A%20Introducing%20UTF-8%20support%20for%20SQL%20Server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-747705%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Pedro%2C%3C%2FP%3E%3CP%3ENice%20write%20up.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20of%20now%2C%20pre-2019%2C%20XML%20data%20type%20supports%20UTF-16%20only.%3C%2FP%3E%3CP%3EAnother%20current%20limitation%20is%20that%20XML%20prolog%20is%20stripped%20while%20inserting%20XML%20with%20it%20into%20a%20column%20or%20a%20variable%20of%20XML%20data%20type.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20about%20UTF-8%20support%20for%20the%20XML%20data%20type%20in%20SQL%20Server%202019%3F%3C%2FP%3E%3CP%3EPlease%20shed%20some%20light%20on%20the%20subject.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-931514%22%20slang%3D%22en-US%22%3ERe%3A%20Introducing%20UTF-8%20support%20for%20SQL%20Server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-931514%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F368010%22%20target%3D%22_blank%22%3E%40ykhabins%3C%2FA%3E%20at%20this%20point%2C%20XML%20data%20type%20is%20not%20supported%20in%20the%20XML%20data%20type.%20And%20there%20are%20no%20current%20plans%20to%20extend%20that%20support.%20The%20XML%20data%20type%20is%20stored%20in%20binary%20format%20in%20SQL%20Server%2C%20so%20the%20perf%20gains%20would%20be%20limited%20if%20any.%20If%20you%20have%20specific%20requirements%20for%20UTF-8%20on%20XML%2C%20please%20open%20a%20feedback%20item%20in%20%3CA%20href%3D%22https%3A%2F%2Faka.ms%2Fsqlfeedback%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2Fsqlfeedback%3C%2FA%3E%20and%20please%20clarify%20your%20requirement%20there.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1027592%22%20slang%3D%22en-US%22%3ERe%3A%20Introducing%20UTF-8%20support%20for%20SQL%20Server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1027592%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F368010%22%20target%3D%22_blank%22%3E%40ykhabins%3C%2FA%3E%26nbsp%3BI%20think%20Pedro%20meant%20to%20say%20that%20%22%3CSTRONG%3E%3CEM%3EUTF-8%3C%2FEM%3E%3C%2FSTRONG%3E%20is%20not%20supported%20in%20the%20XML%20datatype%22.%20And%20by%20%22XML%20datatype%20is%20stored%20in%20binary%20format%22%2C%20he%20means%20that%20the%20XML%20datatype%20is%20an%20optimized%20type%20that%20reduces%20overall%20size%20by%20doing%20several%20things%2C%20such%20as%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1.%20place%20strings%20into%20a%20dictionary%20so%20that%20they%20only%20exist%20once%20(and%20yes%2C%20here%20they%20will%20be%20stored%20as%20UTF-16)%3C%2FP%3E%3CP%3E2.%20remove%20insignificant%20whitespace%3C%2FP%3E%3CP%3E3.%20if%20using%20an%20XSD%20%2F%20XML%20Schema%20Collection%2C%20attributes%20and%20elements%20designated%20with%20specific%20datatypes%20will%20be%20stored%20in%20the%20native%20binary%20representation%20for%20that%20type%20(when%20no%20XML%20Schema%20Collection%20is%20used%20then%20all%20values%20are%20stored%20as%20UTF-16%20strings)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20one%20example%20of%20this%20format%20as%20found%20in%20the%20overall%20protocol%20specification%20for%20the%20XML%20datatype%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fopenspecs%2Fsql_server_protocols%2Fms-binxml%2Fd5bd1f42-8643-435c-a0df-0ba8680a19ee%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fopenspecs%2Fsql_server_protocols%2Fms-binxml%2Fd5bd1f42-8643-435c-a0df-0ba8680a19ee%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20here%20is%20a%20section%20of%20one%20of%20my%20posts%20where%20I%20show%20that%20for%20XML%20data%20containing%20repeated%20strings%2C%20the%20overall%20data%20size%20(in%20bytes)%20is%20smaller%20in%20the%20native%20XML%20datatype%20than%20even%20for%20VARCHAR%20data%20containing%20the%20same%20XML%20data.%20Of%20course%2C%20if%20the%20XML%20data%20contains%20mostly%20unique%20strings%2C%20then%20it's%20possible%20that%20the%20VARCHAR%20representation%20would%20be%20smaller%2C%20though%20that%20doesn't%20always%20help%20since%20VARCHAR%20can't%20contain%20all%20code%20points.%20Either%20way%2C%20take%20a%20look%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsqlquantumleap.com%2F2019%2F11%2F22%2Fhow-many-bytes-per-character-in-sql-server-a-completely-complete-guide%2F%23xml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsqlquantumleap.com%2F2019%2F11%2F22%2Fhow-many-bytes-per-character-in-sql-server-a-completely-complete-guide%2F%23xml%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EALSO%2C%20if%20by%20UTF-8%20support%20in%20XML%20you%20mean%20the%20encoding%20of%20it%20going%20in%20and%2For%20coming%20out%2C%20then%20that%20might%20be%20a%20different%20story.%26nbsp%3B%20The%20XML%20datatype%20%3CEM%3Edoes%3C%2FEM%3E%26nbsp%3Bcurrently%20(and%20always%20has)%20support%20converting%20most%20encodings%20into%20UTF-16.%20The%20reason%20that%20the%20%3CSTRONG%3E%3C%2FSTRONG%3E%20declaration%20is%20stripped%20off%20is%20that%20it%20isn't%20necessary%20as%20the%20only%20encoding%20it%20can%20be%20internally%20is%20UTF-16%20(and%20it%20could%20be%20a%20conflict%20if%20the%20encoding%20specified%20in%20the%20declaration%20states%20otherwise).%20HOWEVER%2C%20if%20you%20pass%20in%20valid%20bytes%20of%20UTF-8%20(or%20even%20Windows-1252%2C%20etc)%20and%20provide%20the%26nbsp%3B%3CSTRONG%3E%3C%2FSTRONG%3E%20declaration%20which%20states%20the%20encoding%20used%20for%20the%20bytes%20you%20are%20passing%20in%20(%3CEM%3Eand%3C%2FEM%3E%20if%20you%20do%20NOT%20prefix%20the%20string%20literal%20with%20an%20upper-case%20%22N%22%20or%20use%20an%20NVARCHAR%20variable)%20then%20the%20XML%20datatype%20will%20convert%20from%20that%20encoding%20into%20UTF-16.%20This%20is%20supported%20starting%20in%20SQL%20Server%202005.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20a%20StackOverflow%20answer%20of%20mine%20in%20which%20I%20provide%20examples%20of%20doing%20this%20conversion%20from%20VARCHAR%20data%20encoded%20as%20UTF-8%20or%20Windows-1252%20into%20XML%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fa%2F39922862%2F577765%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConverting%20accented%20characters%20in%20varchar()%20to%20XML%20causing%20%E2%80%9Cillegal%20XML%20character%E2%80%9D%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOf%20course%2C%20getting%20the%20data%20out%20as%20UTF-8%20encoded%20bytes%20is%20not%20supported%20%3CEM%3Enatively%3C%2FEM%3E%20until%20SQL%20Server%202019%20when%20you%20can%20use%20%3CSTRONG%3ECONVERT(VARCHAR%2C%20..)%3C%2FSTRONG%3E%20to%20achieve%20that.%20Prior%20to%20SQL%20Server%202019%2C%20you%20can%20create%20a%20T-SQL%20or%20SQLCLR%20scalar%20function%20(i.e.%20UDF)%20to%20return%20a%20%3CSTRONG%3EVARBINARY(MAX)%3C%2FSTRONG%3E%20value%20containing%20the%20UTF-8%20encoded%20bytes%20of%20whatever%20was%20passed%20in.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETake%20care%2C%3C%2FP%3E%3CP%3ESolomon....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-734928%22%20slang%3D%22en-US%22%3EIntroducing%20UTF-8%20support%20for%20SQL%20Server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-734928%22%20slang%3D%22en-US%22%3E%3CP%3ESQL%20Server%202019%20introduces%20support%20for%20the%20widely%20used%20UTF-8%20character%20encoding.%20This%20has%20been%20a%20longtime%20requested%20feature%20and%20can%20be%20set%20as%20a%20database-level%20or%20column-level%20default%20encoding%20for%20Unicode%20string%20data.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EWhy%20did%20we%20need%20UTF-8%20support%3F%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThis%20is%20an%20asset%20for%20companies%20extending%20their%20businesses%20to%20a%20global%20scale%2C%20where%20the%20requirement%20of%20providing%20global%20multilingual%20database%20applications%20and%20services%20is%20critical%20to%20meet%20customer%20demands%2C%20and%20specific%20market%20regulations.%3C%2FP%3E%0A%3CP%3EThe%20benefits%20of%20introducing%20UTF-8%20support%20also%20extend%20to%20scenarios%20where%20legacy%20applications%20require%20internationalization%20and%20use%20inline%20queries%3A%20the%20amount%20of%20changes%20and%20testing%20involved%20to%20convert%20an%20application%20and%20underlying%20database%20to%20UTF-16%20can%20be%20costly%2C%20by%20requiring%20complex%20string%20processing%20logic%20that%20affect%20application%20performance.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EHow%20is%20it%20implemented%3F%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ETo%20limit%20the%20amount%20of%20changes%20required%20for%20the%20above%20scenarios%2C%20UTF-8%20is%20enabled%20in%20existing%20the%20data%20types%20CHAR%20and%20VARCHAR.%20String%20data%20is%20automatically%20encoded%20to%20UTF-8%20when%20creating%20or%20changing%20an%20object%E2%80%99s%20collation%20to%20a%20collation%20with%20the%20%E2%80%9C_UTF8%E2%80%9D%20suffix%2C%20for%20example%20from%20%3CSTRONG%3ELATIN1_GENERAL_100_CI_AS_SC%3C%2FSTRONG%3E%20to%20%3CSTRONG%3ELATIN1_GENERAL_100_CI_AS_SC_UTF8%3C%2FSTRONG%3E.%20Refer%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fcollations%2Fset-or-change-the-database-collation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESet%20or%20Change%20the%20Database%20Collation%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fcollations%2Fset-or-change-the-column-collation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESet%20or%20Change%20the%20Column%20Collation%3C%2FA%3E%20for%20more%20details%20on%20how%20to%20perform%20those%20changes.%20Note%20that%20NCHAR%20and%20NVARCHAR%20remains%20unchanged%20and%20allows%20UCS-2%2FUTF-16%20encoding.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELike%20UTF-16%2C%20UTF-8%20is%20only%20available%20to%20Windows%20collations%20that%20support%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fcollations%2Fcollation-and-unicode-support%23Supplementary_Characters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESupplementary%20Characters%3C%2FA%3E%2C%20as%20introduced%20in%20SQL%20Server%202012.%20You%20can%20see%20all%20available%20UTF-8%20collations%20by%20executing%20the%20following%20command%20in%20your%20SQL%20Server%202019%20instance%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20Name%2C%20Description%20FROM%20fn_helpcollations()%0AWHERE%20Name%20LIKE%20'%25UTF8'%3B%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EFunctional%20comparison%20between%20UTF-8%20and%20UTF-16%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EUTF-8%20and%20UTF-16%20both%20handle%20the%20same%20Unicode%20characters%2C%20and%20both%20are%20variable%20length%20encodings%20that%20require%20up%20to%2032%20bits%20per%20character.%20However%2C%20there%20are%20important%20differences%20that%20drive%20the%20choice%20of%20whether%20to%20use%20UTF-8%20or%20UTF-16%20in%20your%20multilingual%20database%20or%20column%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EUTF-8%20encodes%20the%20common%20ASCII%20characters%20including%20English%20and%20numbers%20using%208-bits.%20ASCII%20characters%20(0-127)%20use%201%20byte%2C%20code%20points%20128%20to%202047%20use%202%20bytes%2C%20and%20code%20points%202048%20to%2065535%20use%203%20bytes.%20The%20code%20points%2065536%20to%201114111%20use%26nbsp%3B%204%20bytes%2C%20and%20represent%20the%20character%20range%20for%20Supplementary%20Characters.%3C%2FLI%3E%0A%3CLI%3EBut%26nbsp%3BUTF-16%20uses%20at%20least%2016-bits%20for%20every%20character%20in%20code%20points%200%20to%2065535%20(available%20in%20UCS-2%20and%20UTF-16%20alike)%2C%20and%20code%20points%2065536%20to%201114111%20use%20the%20same%204%20bytes%20as%20UTF-8.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20table%20below%20outlines%20these%20storage%20boundaries%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%20style%3D%22text-align%3A%20center%3B%22%3E%3CFONT%20size%3D%223%22%3ECode%20Range%20(hexadecimal)%3C%2FFONT%3E%3C%2FTH%3E%0A%3CTH%20style%3D%22text-align%3A%20center%3B%22%3E%3CFONT%20size%3D%223%22%3ECode%20Range%20(decimal)%3C%2FFONT%3E%3C%2FTH%3E%0A%3CTH%20style%3D%22text-align%3A%20center%3B%22%3E%3CFONT%20size%3D%223%22%3EStorage%20bytes%20with%20UTF-8%3C%2FFONT%3E%3C%2FTH%3E%0A%3CTH%20style%3D%22text-align%3A%20center%3B%22%3E%3CFONT%20size%3D%223%22%3EStorage%20bytes%20with%20UTF-16%3C%2FFONT%3E%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E000000%20%E2%80%93%2000007F%20(ASCII)%3C%2FTD%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E0%20-%20127%3C%2FTD%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E1%3C%2FTD%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E2%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%20style%3D%22text-align%3A%20center%3B%22%3E%0A%3CTD%3E000080%20%E2%80%93%2000009F%3CBR%20%2F%3E0000A0%20%E2%80%93%200003FF%3CBR%20%2F%3E000400%20%E2%80%93%200007FF%3C%2FTD%3E%0A%3CTD%3E128%20%E2%80%93%20159%3CBR%20%2F%3E160%20%E2%80%93%201%2C023%3CBR%20%2F%3E1%2C024%20%E2%80%93%202%2C047%3C%2FTD%3E%0A%3CTD%3E2%3C%2FTD%3E%0A%3CTD%3E2%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%20style%3D%22text-align%3A%20center%3B%22%3E%0A%3CTD%3E000800%20%E2%80%93%20003FFF%3CBR%20%2F%3E004000%20%E2%80%93%2000FFFF%3C%2FTD%3E%0A%3CTD%3E2%2C048%20-%2016%2C383%3CBR%20%2F%3E16%2C384%20%E2%80%93%2065%2C535%3C%2FTD%3E%0A%3CTD%3E3%3C%2FTD%3E%0A%3CTD%3E2%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E010000%20%E2%80%93%2003FFFF%3CBR%20%2F%3E040000%20%E2%80%93%2010FFFF%3C%2FTD%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E65%2C536%20%E2%80%93%20262%2C143%3CBR%20%2F%3E262%2C144%20%E2%80%93%201%2C114%2C111%3C%2FTD%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E4%3C%2FTD%3E%0A%3CTD%20style%3D%22text-align%3A%20center%3B%22%3E4%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EPerformance%20differences%20between%20UTF-8%20and%20UTF-16%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIf%20your%20%3CSTRONG%3Edataset%20uses%20primarily%20ASCII%20characters%3C%2FSTRONG%3E%20(which%20represent%20majority%20of%20Latin%20alphabets)%2C%20significant%20storage%20savings%20may%20be%20achieved%20as%20compared%20to%20UTF-16%20data%20types.%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20changing%20an%20existing%20column%20data%20type%20from%20NCHAR(10)%20to%20CHAR(10)%20using%20an%20UTF-8%20enabled%20collation%2C%20translates%20into%20nearly%2050%25%20reduction%20in%20storage%20requirements.%20This%20is%20because%20NCHAR(10)%20requires%2022%20bytes%20for%20storage%2C%20whereas%20CHAR(10)%20requires%2012%20bytes%20for%20the%20same%20Unicode%20string.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20ASCII%20range%2C%20when%20doing%20intensive%20read%2Fwrite%20I%2FO%20on%20UTF-8%20%2C%20%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3Ewe%20measured%20an%20average%2035%25%20performance%20improvement%20over%20UTF-16%20using%20%3C%2FSPAN%3Eclustered%20tables%20with%20a%20non-clustered%20index%20on%20the%20string%20column%2C%20and%20an%20average%2011%25%20performance%20improvement%20over%20UTF-16%20using%20a%20heap.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EWhat%20if%20your%20%3CSTRONG%3Edataset%20is%20not%20predominately%20ASCII%3C%2FSTRONG%3E%3F%26nbsp%3B%3C%2FFONT%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EAbove%20the%20ASCII%20range%2C%20almost%20all%20Latin%20alphabets%2C%20but%20also%20Greek%2C%20Cyrillic%2C%20Coptic%2C%20Armenian%2C%20Hebrew%2C%20Arabic%2C%20Syriac%2C%20T%C4%81na%20and%20N%E2%80%99Ko%20will%20use%202%20bytes%20per%20character%20in%20both%20UTF-8%20and%20UTF-16%20(128%20to%202047).%20Performance%20measurements%20were%20very%20similar%20between%20UTF-8%20and%20UTF-16%20in%20this%20range.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20using%20compute-intensive%20operations%20such%20as%20SORTs%2FMERGE%20joins%20then%20UTF-16%20is%20generally%20better%20than%20UTF-8%20for%20the%20same%20dataset.%20This%20is%20because%20a%20few%20internal%20conversions%20happen%20during%20these%20operations.%20HASH%20joins%2FLIKE%2FInequality%20comparisons%20will%20perform%20slightly%20better%20in%20UTF-8%20for%20the%20same%20dataset.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EBut%20Chinese%2C%20Japanese%2C%20or%20Korean%20characters%20are%20represented%20starting%20in%20the%20range%202048%20to%2065535%2C%20and%20use%203%20bytes%20in%20UTF-8%2C%20but%20only%202%20bytes%20in%20UTF-16.%20If%20your%20dataset%20is%20mostly%20in%20this%20character%20range%20then%20using%20UTF-16%20is%20preferred.%20In%20fact%2C%20we%20measured%20about%2025%25%20performance%20degradation%20for%20intensive%20read%20I%2FO%20when%20a%20dataset%20is%20mostly%20in%20this%20range%2C%20and%20is%20using%20UTF-8%20instead%20of%20UTF-16.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EIn%20the%20Supplementary%20character%20range%20(65536%20to%201114111)%20there%20is%20no%20measurable%20difference%20between%20UTF-8%20and%20UTF-16%20encoding%2C%20both%20from%20a%20storage%20and%20performance%20perspective.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20read%20more%20about%20Unicode%20support%20in%20SQL%20Server%2C%20including%20details%20on%20UTF-8%20support%2C%20see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fcollations%2Fcollation-and-unicode-support%23Unicode_Defn%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EHow%20to%20convert%20to%20UTF-8%3F%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EBefore%20you%20convert%2C%20avoid%20data%20loss%20by%20knowing%20what's%20the%20data%20type%20size%20you%20must%20convert%20to.%20For%20example%2C%20if%20a%20column%20were%20defined%20originally%20as%20nvarchar(100)%2C%20and%20the%20projected%20byte%20size%20for%20the%20column%20in%20UTF-8%20would%20be%20120%2C%20then%20changing%20the%20column%20type%20to%20a%20varchar(100)%20would%20result%20in%20data%20loss.%20The%20column%20would%20have%20to%20be%20defined%20at%20least%20as%20varchar(120).%20The%20T-SQL%20script%20or%20the%20SQL%20Notebook%20in%20the%20%3C%2FSPAN%3E%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20underline%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fsql-server-samples%2Fblob%2Fmaster%2Fsamples%2Ffeatures%2Funicode%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EData%20Samples%20GitHub%3C%2FA%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%20helps%20you%20assess%20these%20requirements.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETwo%20popular%20methods%20to%20convert%20data%20are%20described%20next.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E1)%20%3CSTRONG%3EConvert%20column%20data%3C%2FSTRONG%3E.%20Imagine%20you%20have%20a%20current%20table%20that%20is%20encoded%20in%20UCS-2%2FUTF-16%20(d%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3Eepends%20on%20whether%20a%20supplementary%20character%20enabled%20collation%20is%20used%3C%2FSPAN%3E)%20or%20non-Unicode%2C%20like%20the%20following%20examples%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ECREATE%20TABLE%20dbo.MyTable%20(MyString%20NVARCHAR(50)%20COLLATE%20Latin1_General_100_CI_AI_SC)%3B%0A%20%0ACREATE%20TABLE%20dbo.MyTable%20(MyString%20VARCHAR(50)%20COLLATE%20Latin1_General_100_CI_AI)%3B%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3ETo%20convert%20it%20in-place%2C%20run%20an%20alter%20statement%2C%20like%20the%20following%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EALTER%20TABLE%20dbo.MyTable%20%0AALTER%20COLUMN%20MyString%20VARCHAR(50)%20COLLATE%20Latin1_General_100_CI_AI_SC_UTF8%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt's%20very%20easy%20to%20implement%2C%20however%20this%20is%20a%20possibly%20blocking%20operation%20which%20may%20pose%20an%20issue%20for%20large%20tables%20and%20busy%20applications.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2)%20%3CSTRONG%3ECopy%20and%20Replace%3C%2FSTRONG%3E.%20This%20method%20involves%20copying%20data%20to%20a%20new%20table%20where%20the%20target%20column(s)%20are%20already%20in%20UTF-8%2C%20and%20then%20replace%20the%20old%20table.%20The%20following%20T-SQL%20example%20illustrates%20this%20using%20the%20second%20example%20table%20above%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ECREATE%20TABLE%20dbo.MyTable2%20(VARCHAR(50)%20COLLATE%20Latin1_General_100_CI_AI_SC_UTF8)%3B%0AINSERT%20INTO%20dbo.MyTable2%20%0ASELECT%20*%20FROM%20dbo.MyTable%3B%0ADROP%20TABLE%20dbo.MyTable%3B%0AEXEC%20sp_rename%20'dbo.MyTable2'%2C%20'dbo.MyTable%E2%80%99%3B%0A%3C%2FPRE%3E%0A%3CP%3EThis%20method%20is%20much%20faster%2C%20but%20handling%20complex%20schemas%20with%20many%20dependencies%20(FKs%2C%20PKs%2C%20Triggers%2C%20DFs)%20and%20tail%20of%20the%20table%20synch%20requires%20much%20more%20preparation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20conclusion%2C%20keep%20the%20topics%20discussed%20here%20in%20mind%20when%20choosing%20what%20encoding%20to%20use%20in%20your%20database%20or%20columns.%20Especially%20in%20terms%20of%20what%20is%20the%20predominant%20language%2Fcharacter%20set%20that's%20expected%20to%20store%20in%20a%20specific%20table%20column.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20font-family%3A%20'SegoeUI'%2C'Lato'%2C'Helvetica%20Neue'%2CHelvetica%2CArial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EPedro%20Lopes%20(%20%3C%2FSPAN%3E%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20underline%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20href%3D%22https%3A%2F%2Ftwitter.com%2FSQLPedro%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40SQLPedro%20%3C%2FA%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20font-family%3A%20'SegoeUI'%2C'Lato'%2C'Helvetica%20Neue'%2CHelvetica%2CArial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E)%20%E2%80%93%20Principal%20Program%20Manager%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-734928%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3ESQL%20Server%202019%20introduces%20support%20for%20the%20widely%20used%20UTF-8%20character%20encoding.%20This%20has%20been%20a%20longtime%20requested%20feature%20and%20can%20be%20set%20as%20a%20database-level%20or%20column-level%20default%20encoding%20for%20Unicode%20string%20data.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-734928%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

SQL Server 2019 introduces support for the widely used UTF-8 character encoding. This has been a longtime requested feature and can be set as a database-level or column-level default encoding for Unicode string data. 

 

Why did we need UTF-8 support?

This is an asset for companies extending their businesses to a global scale, where the requirement of providing global multilingual database applications and services is critical to meet customer demands, and specific market regulations.

The benefits of introducing UTF-8 support also extend to scenarios where legacy applications require internationalization and use inline queries: the amount of changes and testing involved to convert an application and underlying database to UTF-16 can be costly, by requiring complex string processing logic that affect application performance. 

 

How is it implemented?

To limit the amount of changes required for the above scenarios, UTF-8 is enabled in existing the data types CHAR and VARCHAR. String data is automatically encoded to UTF-8 when creating or changing an object’s collation to a collation with the “_UTF8” suffix, for example from LATIN1_GENERAL_100_CI_AS_SC to LATIN1_GENERAL_100_CI_AS_SC_UTF8. Refer to Set or Change the Database Collation and Set or Change the Column Collation for more details on how to perform those changes. Note that NCHAR and NVARCHAR remains unchanged and allows UCS-2/UTF-16 encoding.

 

Like UTF-16, UTF-8 is only available to Windows collations that support Supplementary Characters, as introduced in SQL Server 2012. You can see all available UTF-8 collations by executing the following command in your SQL Server 2019 instance:

 

SELECT Name, Description FROM fn_helpcollations()
WHERE Name LIKE '%UTF8';

 

Functional comparison between UTF-8 and UTF-16

UTF-8 and UTF-16 both handle the same Unicode characters, and both are variable length encodings that require up to 32 bits per character. However, there are important differences that drive the choice of whether to use UTF-8 or UTF-16 in your multilingual database or column:

 

  • UTF-8 encodes the common ASCII characters including English and numbers using 8-bits. ASCII characters (0-127) use 1 byte, code points 128 to 2047 use 2 bytes, and code points 2048 to 65535 use 3 bytes. The code points 65536 to 1114111 use  4 bytes, and represent the character range for Supplementary Characters.
  • But UTF-16 uses at least 16-bits for every character in code points 0 to 65535 (available in UCS-2 and UTF-16 alike), and code points 65536 to 1114111 use the same 4 bytes as UTF-8. 

The table below outlines these storage boundaries:

 

Code Range (hexadecimal) Code Range (decimal) Storage bytes with UTF-8 Storage bytes with UTF-16
000000 – 00007F (ASCII) 0 - 127 1 2
000080 – 00009F
0000A0 – 0003FF
000400 – 0007FF
128 – 159
160 – 1,023
1,024 – 2,047
2 2
000800 – 003FFF
004000 – 00FFFF
2,048 - 16,383
16,384 – 65,535
3 2
010000 – 03FFFF
040000 – 10FFFF
65,536 – 262,143
262,144 – 1,114,111
4 4

 

Performance differences between UTF-8 and UTF-16

If your dataset uses primarily ASCII characters (which represent majority of Latin alphabets), significant storage savings may be achieved as compared to UTF-16 data types.

For example, changing an existing column data type from NCHAR(10) to CHAR(10) using an UTF-8 enabled collation, translates into nearly 50% reduction in storage requirements. This is because NCHAR(10) requires 22 bytes for storage, whereas CHAR(10) requires 12 bytes for the same Unicode string.

 

In the ASCII range, when doing intensive read/write I/O on UTF-8 , we measured an average 35% performance improvement over UTF-16 using clustered tables with a non-clustered index on the string column, and an average 11% performance improvement over UTF-16 using a heap.

 

What if your dataset is not predominately ASCIIAbove the ASCII range, almost all Latin alphabets, but also Greek, Cyrillic, Coptic, Armenian, Hebrew, Arabic, Syriac, Tāna and N’Ko will use 2 bytes per character in both UTF-8 and UTF-16 (128 to 2047). Performance measurements were very similar between UTF-8 and UTF-16 in this range.

 

When using compute-intensive operations such as SORTs/MERGE joins then UTF-16 is generally better than UTF-8 for the same dataset. This is because a few internal conversions happen during these operations. HASH joins/LIKE/Inequality comparisons will perform slightly better in UTF-8 for the same dataset.

 

But Chinese, Japanese, or Korean characters are represented starting in the range 2048 to 65535, and use 3 bytes in UTF-8, but only 2 bytes in UTF-16. If your dataset is mostly in this character range then using UTF-16 is preferred. In fact, we measured about 25% performance degradation for intensive read I/O when a dataset is mostly in this range, and is using UTF-8 instead of UTF-16. 

 

In the Supplementary character range (65536 to 1114111) there is no measurable difference between UTF-8 and UTF-16 encoding, both from a storage and performance perspective.

 

To read more about Unicode support in SQL Server, including details on UTF-8 support, see here.

 

How to convert to UTF-8?

Before you convert, avoid data loss by knowing what's the data type size you must convert to. For example, if a column were defined originally as nvarchar(100), and the projected byte size for the column in UTF-8 would be 120, then changing the column type to a varchar(100) would result in data loss. The column would have to be defined at least as varchar(120). The T-SQL script or the SQL Notebook in the Data Samples GitHub helps you assess these requirements.

 

Two popular methods to convert data are described next. 

1) Convert column data. Imagine you have a current table that is encoded in UCS-2/UTF-16 (depends on whether a supplementary character enabled collation is used) or non-Unicode, like the following examples:

 

CREATE TABLE dbo.MyTable (MyString NVARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC);
 
CREATE TABLE dbo.MyTable (MyString VARCHAR(50) COLLATE Latin1_General_100_CI_AI);


To convert it in-place, run an alter statement, like the following example:

 

ALTER TABLE dbo.MyTable 
ALTER COLUMN MyString VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8

 

It's very easy to implement, however this is a possibly blocking operation which may pose an issue for large tables and busy applications.

 

2) Copy and Replace. This method involves copying data to a new table where the target column(s) are already in UTF-8, and then replace the old table. The following T-SQL example illustrates this using the second example table above:

 

CREATE TABLE dbo.MyTable2 (VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8);
INSERT INTO dbo.MyTable2 
SELECT * FROM dbo.MyTable;
DROP TABLE dbo.MyTable;
EXEC sp_rename 'dbo.MyTable2', 'dbo.MyTable’;

This method is much faster, but handling complex schemas with many dependencies (FKs, PKs, Triggers, DFs) and tail of the table synch requires much more preparation.

 

In conclusion, keep the topics discussed here in mind when choosing what encoding to use in your database or columns. Especially in terms of what is the predominant language/character set that's expected to store in a specific table column.

 

Pedro Lopes ( @SQLPedro ) – Principal Program Manager

4 Comments
Occasional Visitor

A nice clear and helpful article. Thanks!

New Contributor

Hi Pedro,

Nice write up.

 

As of now, pre-2019, XML data type supports UTF-16 only.

Another current limitation is that XML prolog is stripped while inserting XML with it into a column or a variable of XML data type.

 

What about UTF-8 support for the XML data type in SQL Server 2019?

Please shed some light on the subject.

Microsoft

@ykhabins at this point, XML data type is not supported in the XML data type. And there are no current plans to extend that support. The XML data type is stored in binary format in SQL Server, so the perf gains would be limited if any. If you have specific requirements for UTF-8 on XML, please open a feedback item in https://aka.ms/sqlfeedback and please clarify your requirement there.

@ykhabins I think Pedro meant to say that "UTF-8 is not supported in the XML datatype". And by "XML datatype is stored in binary format", he means that the XML datatype is an optimized type that reduces overall size by doing several things, such as:

 

1. place strings into a dictionary so that they only exist once (and yes, here they will be stored as UTF-16)

2. remove insignificant whitespace

3. if using an XSD / XML Schema Collection, attributes and elements designated with specific datatypes will be stored in the native binary representation for that type (when no XML Schema Collection is used then all values are stored as UTF-16 strings)

 

Here is one example of this format as found in the overall protocol specification for the XML datatype:

https://docs.microsoft.com/en-us/openspecs/sql_server_protocols/ms-binxml/d5bd1f42-8643-435c-a0df-0b...

 

And here is a section of one of my posts where I show that for XML data containing repeated strings, the overall data size (in bytes) is smaller in the native XML datatype than even for VARCHAR data containing the same XML data. Of course, if the XML data contains mostly unique strings, then it's possible that the VARCHAR representation would be smaller, though that doesn't always help since VARCHAR can't contain all code points. Either way, take a look:

 

https://sqlquantumleap.com/2019/11/22/how-many-bytes-per-character-in-sql-server-a-completely-comple...

 

ALSO, if by UTF-8 support in XML you mean the encoding of it going in and/or coming out, then that might be a different story.  The XML datatype does currently (and always has) support converting most encodings into UTF-16. The reason that the <?xml ?> declaration is stripped off is that it isn't necessary as the only encoding it can be internally is UTF-16 (and it could be a conflict if the encoding specified in the declaration states otherwise). HOWEVER, if you pass in valid bytes of UTF-8 (or even Windows-1252, etc) and provide the <?xml ?> declaration which states the encoding used for the bytes you are passing in (and if you do NOT prefix the string literal with an upper-case "N" or use an NVARCHAR variable) then the XML datatype will convert from that encoding into UTF-16. This is supported starting in SQL Server 2005.

 

Here is a StackOverflow answer of mine in which I provide examples of doing this conversion from VARCHAR data encoded as UTF-8 or Windows-1252 into XML:

Converting accented characters in varchar() to XML causing “illegal XML character”

 

Of course, getting the data out as UTF-8 encoded bytes is not supported natively until SQL Server 2019 when you can use CONVERT(VARCHAR, ..) to achieve that. Prior to SQL Server 2019, you can create a T-SQL or SQLCLR scalar function (i.e. UDF) to return a VARBINARY(MAX) value containing the UTF-8 encoded bytes of whatever was passed in.

 

Take care,

Solomon....