# Unicode Support for MS SQL Server

**URL:** <https://www.ruby-forum.com/t/unicode-support-for-ms-sql-server/129721>\
**Category:** Rails\
**Created:** [February 21, 2008, 12:32am UTC](https://www.ruby-forum.com/t/unicode-support-for-ms-sql-server/129721 "2008-02-21T00:32:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 12:32am UTC](https://www.ruby-forum.com/t/unicode-support-for-ms-sql-server/129721/1 "2008-02-21T00:32:35Z")

</div>

In order to coerce Rails 1.2.x into supporting Unicode values for an  
attribute in a model, I found I had to do the following:

1. Change the underlying column type from varchar to nvarchar.

2. Prepend any quoted values going into nvarchar columns with a  
capital-letter-N, e. g. ‘unicode’ becomes N’unicode’ In order to  
accomplish this, I modified SQLServerAdapter#quote to look like this:

%H:%M:%S")}’"  
when Date then “’#{value.strftime(”%Y%m  
%d")}’"  
when String, ActiveSupport::Multibyte::Chars  
qvalue = super  
qvalue = “N#{qvalue}” if !column.nil? && column.sql\_type  
=~ /\Anvarchar/  
qvalue  
else  
super  
end  
end

This passed the several tests I wrote for it and otherwise seemed to  
pass the couple of AR tests I ran against it. (There’s another nice  
bug in the SQL Server adapter that puts the value ‘NULL’ into string  
columns instead of the actual value NULL which causes the polymorphic  
model tests to fail.)

My questions are:

1. Does this look right?

2. Can this be improved?

3. Can this be moved into a Rails plugin so it isn’t a monkey patch to  
AR?

4. Has someone else done this already?

Thanks.

Michael
