NCHAR (Transact-SQL)

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)

Unicode 標準の定義に従って、指定された整数コードの Unicode 文字を返します。

Transact-SQL 構文表記規則

構文

NCHAR ( integer_expression )  

Note

SQL Server 2014 (12.x) 以前のバージョンの Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。

引数

integer_expression
データベースの照合順序に補助文字 (SC) フラグが含まれない場合、これは 0 から 65535 (0 から 0xFFFF) までの正の整数です。 この範囲外の値を指定すると NULL が返されます。 補助文字の詳細については、次を参照してください。 照合順序と Unicode のサポートです。

データベースの照合順序が SC フラグをサポートする場合、これは 0 から 1114111 (0 から 0x10FFFF) までの正の整数です。 この範囲外の値を指定すると NULL が返されます。

戻り値の型

既定のデータベースの照合順序が補助文字をサポートしない場合は nchar(1)

既定のデータベースの照合順序が補助文字をサポートする場合は nvarchar(2)

パラメーター integer_expression が 0 ~ 0 xFFFF の範囲内にある場合、1 つの文字だけが返されます。 値が大きい場合、NCHAR から対応するサロゲート ペアが返されます。 NCHAR(<High surrogate>) + NCHAR(\<Low Surrogate>) を使用してサロゲート ペアを作成しないでください。 代わりに、補助文字をサポートするデータベースの照合順序を使用して、サロゲート ペアの Unicode コードポイントを指定します。 次の例では、サロゲート ペアを構築する古いスタイルの方法と Unicode コードポイントを指定する推奨される方法の両方を示しています。

CREATE DATABASE test COLLATE Finnish_Swedish_100_CS_AS_SC;  
DECLARE @d NVARCHAR(10) = N'𣅿';
-- Old style method.  
SELECT NCHAR(0xD84C) + NCHAR(0xDD7F);   
  
-- Preferred method.   
SELECT NCHAR(143743);   
  
-- Alternative preferred method.  
SELECT NCHAR(UNICODE(@d));    

A. NCHAR と UNICODE を使用する

次の例では、UNICODE 関数と NCHAR 関数を使用して、UNICODE という文字列の 2 番目の文字の NCHAR 値と København (Unicode 文字) を出力することによって、ø という実際の 2 番目の文字を出力します。

DECLARE @nstring NCHAR(8);  
SET @nstring = N'København';  
SELECT UNICODE(SUBSTRING(@nstring, 2, 1)),   
   NCHAR(UNICODE(SUBSTRING(@nstring, 2, 1)));  
GO  

結果セットは次のようになります。

----------- -   
248         ø  
(1 row(s) affected)  

B. SUBSTRING、UNICODE、CONVERT、および NCHAR を使用する

次の例では、SUBSTRING 関数、UNICODE 関数、CONVERT 関数、および NCHAR 関数を使用して、København という文字列の中の各文字の文字番号、Unicode 文字、および UNICODE 値を出力します。

-- The @position variable holds the position of the character currently  
-- being processed. The @nstring variable is the Unicode character   
-- string to process.  
DECLARE @position INT, @nstring NCHAR(9);  
-- Initialize the current position variable to the first character in   
-- the string.  
SET @position = 1;  
-- Initialize the character string variable to the string to process.  
-- Notice that there is an N before the start of the string. This   
-- indicates that the data following the N is Unicode data.  
SET @nstring = N'København';  
-- Print the character number of the position of the string you are at,   
-- the actual Unicode character you are processing, and the UNICODE   
-- value for this particular character.  
PRINT 'Character #' + ' ' + 'Unicode Character' + ' ' + 'UNICODE Value';  
WHILE @position <= DATALENGTH(@nstring)  
   BEGIN  
   SELECT @position,   
      NCHAR(UNICODE(SUBSTRING(@nstring, @position, 1))),  
      CONVERT(NCHAR(17), SUBSTRING(@nstring, @position, 1)),  
      UNICODE(SUBSTRING(@nstring, @position, 1))  
   SELECT @position = @position + 1  
   END;  
GO  

結果セットは次のようになります。

Character # Unicode Character UNICODE Value  
  
----------- ---- ----------------- -----------   
1           K    K                 75  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
2           ø    ø                 248  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
3           b    b                 98  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
4           e    e                 101  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
5           n    n                 110  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
6           h    h                 104  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
7           a    a                 97  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
8           v    v                 118  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
9           n    n                 110  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
10          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
11          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
12          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
13          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
14          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
15          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
16          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
17          NULL                   NULL  
  
(1 row(s) affected)  
  
----------- ---- ----------------- -----------   
18          NULL                   NULL  
  
(1 row(s) affected)  

参照

ASCII (Transact-SQL)
CHAR (Transact-SQL)
UNICODE (Transact-SQL)
データ型 (Transact-SQL)
文字列関数 (Transact-SQL)