3

I have a string in UTF8 like:

Thermal Oxide: 0 Å to 40 μm

and I want to save it in SQL Server.

When I save it in data type ntext/nvarchar - it saves like this:

Thermal Oxide: 0 ? to 40 ?m

What can I do?

2
  • 1
    It all depends on how you (un)save it. Commented Feb 23, 2012 at 11:22
  • HOW are you saving it?? Nvarchar is definitely able to store this string - no problem at all. Commented Feb 23, 2012 at 11:26

2 Answers 2

12

Precede the quoted string with N:

INSERT INTO someTable (myField) VALUES (N'Thermal Oxide: 0 Å to 40 μm')
Sign up to request clarification or add additional context in comments.

1 Comment

Why the sudden downvote? If I could improve the answer I would love to know how.
4

I have a string in UTF8 like:

Actually, no you don't - you have a unicode string. UTF8, however, only applies to how it is encoded, i.e. storage. If you mean you want to store it, fine; simply: make sure the column you store it in is nvarchar(somelen) (or similar), and that you use parameters to store it. If you just use cmd.Parameters.AddWithValue("paramName", yourString); that'll be fine (for TSQL like insert [tablename] (...fields...) values (..., @foo) etc). Likewise, any ORM / micro-ORM / other database tool will work fine with this, as long as the column is nvarchar(somelen) or similar. For example, with "dapper":

string s = "Thermal Oxide: 0 Å to 40 μm";
connection.Execute("insert [tablename] (colname) values (@s)", new {s});

2 Comments

Hi Marc! Why new {s} as opposed to simply s? Also, the string s has a preceding single-quote but no close single-quote.
@dotancohen the single quote shouldn't have been there, sorry. Re "simply s" - dapper uses that as the parameter name; new {s} represents an anonymous type with a string member s with value "Thermal ..." - everything we need to run it as a parameterised command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.