# SQL Server UCS-2 and UTF-8

**URL:** <https://www.ruby-forum.com/t/sql-server-ucs-2-and-utf-8/146088>\
**Category:** Rails\
**Created:** [August 17, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/sql-server-ucs-2-and-utf-8/146088 "2008-08-17T19:41:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [August 17, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/sql-server-ucs-2-and-utf-8/146088/1 "2008-08-17T19:41:14Z")

</div>

Hello all,

I am writing data into an MS SQL Server, to be used by other  
applications (not my rails app).

The problem is I need to put Chinese and Japanese characters into the  
database. If I write it in using the Rails SQL Server adapter, it  
writes the data in UTF-8 code points, which rails can then read again  
and deal with fine.

Problem is, other applications see this as garbage as it is not UCS-2.

I’m trying to find a way to write in UCS-2. The following code  
snippet does not work:

require ‘iconv’

def convert(string)  
Iconv.iconv(‘UCS-2’, ‘UTF-8’, string)  
end

MsSqlModel.update\_attribute(:name =\> convert(name))

Which is sort of expected as the SQL adapter does not N’’ quote it’s  
UTF-8 strings. So I tried the following (which also doesn’t work)…

require ‘iconv’

def convert(string)  
Iconv.iconv(‘UCS-2’, ‘UTF-8’, string)  
end

MsSqlModel.connection.execute(“UPDATE table\_name SET name =  
N’#{convert(name)}’;”)

Has anyone out there had and solved this problem? Extensive googling  
has turned up very little.

Thanks all

Mikel
