Inserting special characters (greater/less than or equal symbol) into SQL Server database

你。 提交于 2019-12-07 01:37:21

问题


I am trying to insert and into a symbol table where the column is of type nvarchar.

Is this possible or are these symbols not allowed in SQL Server?


回答1:


To make it work, prefix the string with N

create table symboltable 
(
  val nvarchar(10)
)

insert into symboltable values(N'≥') 

select *
from symboltable 

Further Reading:

  • You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server
  • Why do some SQL strings have an 'N' prefix?



回答2:


To add to gonzalo's answer, both the string literal and the field need to support unicode characters.

String Literal

Per Marc Gravell's answer on What does N' stands for in a SQL script ?:

'abcd' is a literal for a [var]char string, occupying 4 bytes memory, and using whatever code-page the SQL server is configured for.
N'abcd' is a literal for a n[var]char string, occupying 8 bytes of memory, and using UTF-16.

Where the N prefix stands for "National" Language in the SQL-92 standard and is used for representing unicode characters. For example, in the following code, any unicode characters in the basic string literal are first encoded into SQL Server's "code page":

Aside: You can check your code page with the following SQL:

SELECT DATABASEPROPERTYEX('dbName', 'Collation') AS dbCollation;
SELECT COLLATIONPROPERTY( 'SQL_Latin1_General_CP1_CI_AS' , 'CodePage' ) AS [CodePage];

The default is Windows-1252 which only contains these 256 characters

Field Type

Once the values are capable of being passed, they'll also need to be capable of being stored into a column that supports unicode types, for example:

  • nchar
  • nvarchar
  • ntext

Further Reading:

  • Why do we need to put N before strings in Microsoft SQL Server?
  • What is the meaning of the prefix N in T-SQL statements?
  • You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server
  • Why do some SQL strings have an 'N' prefix?


来源:https://stackoverflow.com/questions/12028594/inserting-special-characters-greater-less-than-or-equal-symbol-into-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!