Search code examples
sql-servernvarchar

sql server not handling non-ascii characters


Our platform's database has stopped accepting non-ASCII characters despite it working before and despite full use of nvarchar type. The minimal reproduction I've been able to do is this simple demonstration from a Management Studio query window:

DECLARE @X nvarchar(max) = 'Activbasē ਪਾਕਿਸਤਾਨ ਪੰਜਾਬ ਦਾ ਦਾਰੁਲ'
PRINT @X
SELECT @X AS X
-- Output (for table and PRINT): Activbase ???????? ????? ?? ?????

I'm using:

  1. SQL Server version: Microsoft SQL Server 2017 (RTM-GDR) (KB4505224) - 14.0.2027.2 (X64)
  2. Management Studio version: v18.4 (component version 15.0.18206.0)

(both latest versions to date)

Query Options for the query window for ANSI are as follows:

  • SET QUOTED_IDENTIFER = on
  • SET ANSI_NULL_DFLT_ON = on
  • SET ANSI_PADDING = on
  • SET ANSI_WARNINGS = on
  • SET ANSI_NULLS = on

  • SET IMPLICIT_TRANSACTIONS = off

  • SET CURSOR_CLOSE_ON_COMMIT = off

Thanks in advance.


Solution

  • You've made a tiny mistake, you've missed the N prefix to show that your string is Unicode (National language character set):

    DECLARE @X nvarchar(max) = N'Activbasē ਪਾਕਿਸਤਾਨ ਪੰਜਾਬ ਦਾ ਦਾਰੁਲ'
    PRINT @X
    SELECT @X AS X
    

    Output:

    Activbasē ਪਾਕਿਸਤਾਨ ਪੰਜਾਬ ਦਾ ਦਾਰੁਲ