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-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%20noopener%20noreferrer%20noopener%20noreferrer%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%20noopener%20noreferrer%20noopener%20noreferrer%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%20noopener%20noreferrer%20noopener%20noreferrer%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%20noopener%20noreferrer%20noopener%20noreferrer%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%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%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%20Senior%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-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%22%3Ehttps%3A%2F%2Faka.ms%2Fsqlfeedback%3C%2FA%3E%20and%20please%20clarify%20your%20requirement%20there.%3C%2FP%3E%3C%2FLINGO-BODY%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?

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 ) – Senior Program Manager

3 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.