The common misconception that the argument to NVARCHAR(x)
is a character count has bitten me many times today. I am asking this question in the interest of exploring the topic further.
Consider an NVARCHAR(max)
column. Call it FOO_PERM_COL
from table FOO_PERM_TABLE
. Will the following code ever fail?
CREATE TABLE #FOO_TEMP_TABLE
(
FOO_TEMP_COL NVARCHAR(1000) NOT NULL
)
INSERT INTO #FOO_TEMP_TABLE
SELECT Left(FOO_PERM_COL, 1000)
FROM FOO_PERM_TABLE
If the code can fail (i.e. throw an exception), I'd like to see some examples that make it fail. If collations matter, then please say so. If the code cannot fail, then I'd like the argument supporting this to be backed up with reference to the documentation.
I expect that LEFT
's documentation does not tell the full story. The documentation says that it trims your column down to the specified number of characters, but this dbfiddle has me suspect that it does more than that. If LEFT
only cut down to the specified number of characters, then I would expect SELECT LEFT(N'🙏', 1)
to return the unicode character that you put in. Dbfiddle says that it doesn't. I think that the following line in the documentation is of the utmost relevance to that point
The integer_expression parameter counts a UTF-16 surrogate character as one character.
1条答案
按热度按时间wvyml7n51#
When you create a table having columns with litteral datatypes (CHAR, NCHAR, VARCHAR, NVARCHAR) the database collation is used to enforce the colum datatype.
You can see that in :
SQL Server use UTF16 encoding that have no emitcon by default, but can add emoticons only if you use a SC (Supplmential Characters) collation.
List of SC collations can be found with :
As a test :
Will return : �
Will RETURN 🙏
CQFD