Skip to content

Storing unicode characters in SQL Server #215

@jnolis

Description

@jnolis

I'm trying to write Unicode strings from R to SQL. Unfortunately, the Unicode characters only seem to work when I load the table back into R, and not when I view the table in SSMS or A different tool.

require(odbc)
require(DBI)
require(dplyr)
con <- DBI::dbConnect(odbc::odbc(),
    .connection_string = "DRIVER={ODBC Driver 13 for SQL Server};SERVER=Test; Database=Test; trusted_connection=yes;")
testData <- data_frame(Characters = "")
dbWriteTable(con,"TestUnicode",testData,overwrite=TRUE)
result <- dbReadTable(con, "TestUnicode")

Successfully yields:

> result$Characters
[1] ""

However, when I pull that table in SSMS:

SELECT * FROM TestUnicode

I get two different characters:

Characters
â¤

It looks like the column stored in SQL is a varchar rather than nvarchar, and somehow the binary value of the particular heart character ends up being different than what I get when I insert a row from SSMS. Can the unicode characters be stored in a way that they can be used universally and not just in R?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions