convert/cast of nvarchar

nofu1

Active member
Joined
Oct 30, 2020
Messages
37
Programming Experience
Beginner
Hello all;

This has been puzzling me for a while. I have a table structure similar to this below to hold french word
C#:
create table metest
(
 id numeric
 ,desc_info nvarchar

)

Hence, i know nvarchar tends to store the data as bytes, thus when i write select id, desc_info from metest, how do i ensure desc_info shows the french symbols. i am not see the french symbol like (' )instead i am seeing some strange symbot
 
There is nothing to do between your application and your database. .NET strings are Unicode by default. If you save the correct data then you will retrieve the correct data. If you're not seeing the correct data then you are breaking it somewhere, either before saving or after retrieving. We have no idea what you do on either end so we have no idea where it's breaking. I suggest that you start by debugging your app at both ends to make sure that the data is what you expect it to be at all points and, if it's not, seeing where it is not. If you still can't solve the issue, at least you can provide us with all the relevant information.
 
i know nvarchar tends to store the data as bytes
You don't know that at all. At the lowest level, all data is stored as bytes because that's all a computer can store. The nvarchar data type is no different to any other in that regard. At a higher level though, nvarchar data is stored as text, just like varchar data is. In either case, you simply save or retrieve a String in your .NET code and the database takes care of any encoding details based on the data type.
 
You don't know that at all. At the lowest level, all data is stored as bytes because that's all a computer can store. The nvarchar data type is no different to any other in that regard. At a higher level though, nvarchar data is stored as text, just like varchar data is. In either case, you simply save or retrieve a String in your .NET code and the database takes care of any encoding details based on the data type.
Hi jmcihinney whenever I write a query in SSMS, i tend to see a strange symbol for the french character, how do i get to see the actual symbol instead. please note, this is not a .net code
 
This site is dedicated to C# programming and the SQL Server forum is intended for C# questions that relate to SQL Server. The description for this forum is:
Discussion related to SQL Server and MSDE with C# development
I won't close this thread but keep that in mind for the future. You may find a dedicated SQL Server site more appropriate for dedicated SQL Server questions. It's also important to always provide all the relevant information. If your issue is specific to queries in SSMS than that is relevant information.
 
OK I didn't know, so my question is in regards to the following example, I can save the following word s'il vous plaît in my nvarchar(2000) column which is called desc_info but when I write a select * from metest, the word doesn't display correctly in SSMS. How do I get it to show s'il vous plaît when i write a select statement in SSMS
 
I just copied the French text from your post into a table using SSMS and I have no issue seeing the accented character. What version of SQL Server and SSMS are you using and what is the collation of your database?
 
Last edited:
From my experience, the collation is the key setting to look at when dealing with internationalization and SQL Server.
 
I just copied the French text from your post into a table using SSMS and I have no issue seeing the accented character. What version of SQL Server and SSMS are you using and what is the collation of your database?
I am using Microsoft sql server 2019 - 15.0, SSMS 18.7.1 and Latin1_General_CI_AS but i tried changing it to a different collate in the query like this below

C#:
select id, desc_info collate Latin1_General from metest

but was told it was an invalid collate
 
I can save the following word s'il vous plaît in my nvarchar(2000) column which is called desc_info
How exactly are you saving the word? You indicated that you are not using C#, so what language are you using to writing to the database? How does that language handle Unicode strings? And in particular Unicode strings destined to be stored in SQL Server?
 
I copied your text directly into SSMS and had no issues. Can you do that? If you can then it would appear that the issue is indeed how you're getting the data into the database otherwise, so any talk about SSMS wouldn't really be relevant.
 
Sorry about the late reply, the source was a csv file, I was trying out several approach to fix it. and I noticed that i had to redesign my tables to use all nvarchar columns and import the data using SSDT as unicode for it work properly, which I find a bit strange.

just another quick question though,
Requirement
if you have two csv files and one of the csv contains two columns and the other contains 4 columns.

potential solutions
Do you have to create two different tables and ensure your tables all have nvarchar columns and likewise create two different data flow to import it using SSDT into the database

or is there a better way to do it.

Thanks in advance
 
That would imply that person creating the CSV file saved the data into the CSV encoded in a particular code page instead of saving the CSV as UTF-8. You must find that person and beat them about the head and shoulders ask them what code page they used so that you can reload the data into Unicode and then store into the database as Unicode.
 
Back
Top Bottom