Convert ANSI varchars to Unicode nvarchar - sql-server

Due to this I have a table full of varchar values (e.g., 戦艦å¸å›½) that I need to convert to proper unicode nvarchar values (e.g.,戦艦帝国). How can I do that within T-SQL?

Related

Euro sign (€) in VARCHAR column

I have many of my database columns defined as VARCHAR(255) and now would like to input unicode characters (especially the € sign).
Inserting data doesn´t show the unicode symbols, but if I update my insert statement to use NVARCHAR paramerts it is working. But why? I didn´t change the column definition from VARCHAR to NVARCHAR.
To support these symbols should I just change all paramerts from VARCHAR to NVARCHAR or should I also update the columns definition to NVARCHAR(255)?
Varchar datatype doesn't hold Unicode character. You must convert VARCHAR(255)data type to NVARCHAR(255) inorder to hold Unicode character.
Also update the columns definition to NVARCHAR(255)
use NVARCHAR or change your database character set (export, reinstall with a character set that supports the euro sign like WE8ISO8859P15 or AL32UTF8, and import).
Here's an example of NVARCHAR:
link

SSIS Convert m/dd/yyyy to yyyymmdd with inconsistencies

I'm loading many files into a SQL SERVER database. I have one flat file that has a Date Column coming in as string[DT_STR].
I have TWO "date fields" in my database. One is varchar, one is datetime.
Converting the datetime column is no issue, I just use Data Conversion/Derived Column if necessary. However, this varchar column is giving me trouble. Our database values for this column should be in yyyymmdd format. However, on this single file the format of the dates change.
Normally I'd do a SUBSTRING(...) expression here, but the difficulty is that the format of these dates change. some examples of values could be
08/16/2017
8/16/2017
08/6/2017
08/06/2017
10/6/2017
10/06/2017
This makes the challenge harder. I tried LEN([DATE]) == NUM_HERE ? do_THING : OTHER_CALC, but this approach failed because the length of 10/6/2017 is the same as 8/06/2017 which will give me the wrong result. Does anyone have a good workaround for this?
Perhaps a simple convert to date and then into the final format. If 2012+, use try_convert() to trap any bogus dates.
Example
Declare #YourTable Table ([SomeCol] varchar(50))
Insert Into #YourTable Values
('08/16/2017')
,('8/16/2017')
,('08/6/2017')
,('08/06/2017')
,('10/6/2017')
,('10/06/2017')
Select *
,Formatted = convert(varchar(8),convert(Date,SomeCol),112)
from #YourTable
Returns
SomeCol Formatted
08/16/2017 20170816
8/16/2017 20170816
08/6/2017 20170806
08/06/2017 20170806
10/6/2017 20171006
10/06/2017 20171006
Convert the varchar data to datetime and convert that to a formatted string
SELECT CONVERT(varchar,(CONVERT(datetime, '8/6/2017')),112)

How to convert varchar to datetime

I have a varchar column header (Prod_time) in the [YYYYmmdd hhmmss] format that I am trying to convert to datetime. I need to be able to pull data from a certain number of days, and I would like to be able to convert the varchar to datetime to facilitate this.
Is there a way to convert varchar to datetime? No special formatting of the datetime needed, only a data type conversion.
You need to force a couple characters in here so that the convert function knows how to deal with this. We can use STUFF for this pretty easily. This works given the provided string format.
declare #SomeChar varchar(20) = '20170216 100903'
select CONVERT(datetime, STUFF(STUFF(#SomeChar, 12, 0, ':'), 15, 0, ':'))
If at all possible you should consider converting the datatype to a datetime. It eliminates this kind of hassle and also prevents invalid values.

Why I can insert non-ascii characters into VARCHAR column and correctly get it back?

Below is my code sample.
DECLARE #a TABLE (a VARCHAR(20));
INSERT #a
(a)
VALUES ('中');
SELECT *
FROM #a;
I'm using SQL Server Management Studio to run it. My question is, why I can insert non-ascii characters into VARCHAR column and correctly get it back? As I understand, VARCHAR type is only for ascii characters and the NVARCHAR is for unicode characters. Anyone can help to explain it please? I'm on Windows 7 with SQL Server 2014 developer edition.
The codepage used to store the varchar data varies by DB collation.
https://msdn.microsoft.com/en-us/library/ms189617.aspx
Varchar is 8 bits, so you may have a different collation, or you may have gotten lucky on where your character falls on the code set
You can find the ASCII and Extended ASCII characters below.
ASCII
Extended ASCII
I don't believe '中' is an ASCII character.
www.asciitable.com

SQL Server 2005: converting varchar to nvarchar issue

I have table with two varchar columns first_name and last_name.
Now I need to convert these columns to nvarchar in order to support UTF-8.
I choose nvarchar datatype in SSMS for these columns and when I try to enter some UTF-8 data, my symbols converts to question marks. For example, if I input йцукен (Ukrainian) it will be converted to ??????.
What is the problem and how to fix it?
Thanks.
When you want to insert nvarchar literals into the database table, you must use the N'..' prefix.
So use
INSERT INTO dbo.YourTable(First_Name)
VALUES(N'йцукен')
so that this string will be treated as a unicode string
If you're not using the N'..' notation, you're really inserting a non-unicode string literal - and this will cause these conversions to ?

Resources