unicode problem

%3CLINGO-SUB%20id%3D%22lingo-sub-287373%22%20slang%3D%22en-US%22%3Eunicode%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-287373%22%20slang%3D%22en-US%22%3E%3CP%3Ehi%20i%20have%20a%20sql%20server%20with%20a%20NVARCHAR(1000)%20field%20but%20if%20i%20runt%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22_5yl5%22%3E%3CSPAN%3Eselect%20%5BID%5D%2CCONVERT(VARBINARY(1000)%2CAnagrafica)%20from%20TabClienti%3C%2FSPAN%3E%3C%2FSPAN%3E%20the%20field%20contains%20bytes%20instead%20of%20if%20i%20try%20to%20select%20*%20from%20TabClienti%20the%20field%20will%20be%20empty%2C%20what%20can%20do%20for%20export%20data%20in%20LATIN%20or%20ASCII%20format%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-321337%22%20slang%3D%22en-US%22%3ERe%3A%20unicode%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-321337%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20day%20%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20I%20understand%20your%20request%20correctly%2C%20then%20you%20want%20to%20CONVERT%20NVARCHAR%20into%20VARCHAR.%20%3CSTRONG%3EThis%20means%20that%20you%26nbsp%3Bmight%20lose%20data%3C%2FSTRONG%3E%2C%20since%20Unicode%20%3CSPAN%3Eincludes%26nbsp%3B%3C%2FSPAN%3E1%2C114%2C112%20code%20positions%20and%20maps%20over%20100k%20different%20characters%20while%20extended%20ASCII%20only%20include%20256%26nbsp%3B%3CSPAN%3Ecode%20positions%2C%20which%20mean%20it%20cannot%20map%20more%20then%20256%20different%20characters!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOnce%20you%20took%20this%20into%20consideration%26nbsp%3Byou%20might%20find%20the%20following%20trick%26nbsp%3B%3C%2FSPAN%3Ehelpful%3C%2FP%3E%0A%3CPRE%3E%2F*********************************************************%20%20*%2F%0A%2F****************************%20CONVERT%20FROM%20Unicode%20to%20Ascii%20*%2F%0A%2F*********************************************************%20%20*%2F%0A---------------------------------------%20Demo%20table%20(DDL%2BDML)%0ADROP%20TABLE%20IF%20EXISTS%20T%0AGO%0Acreate%20table%20T(txt%20nvarchar(100))%20--%20Unicode%0AGO%0AINSERT%20T(txt)%20VALUES%20(N'%D7%A8%D7%95%D7%A0%D7%9F%20ronen')%20--%20Hebrew%20%26amp%3B%20English%20Characters%20(my%20First%20name)%0AGO%0ASELECT%20*%20FROM%20T%0AGO%0A%0A---------------------------------------%20Solution%20Using%20temp%20table%0ADROP%20TABLE%20IF%20EXISTS%20%23T%3B%0ACREATE%20TABLE%20%23T%20(txt%20VARCHAR(200)%20COLLATE%20Hebrew_CI_AI)%0A--INSERT%20T_NEW%20(txt)%20SELECT%20CONVERT%20(VARCHAR(200)%2C%20txt%20COLLATE%20Hebrew_CI_AI)%0AINSERT%20%23T%20(txt)%20SELECT%20txt%0AFROM%20T%0A%0ASELECT*%20FROM%20T%0ASELECT*%20FROM%20%23T%0AGO%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

hi i have a sql server with a NVARCHAR(1000) field but if i runt

 

select [ID],CONVERT(VARBINARY(1000),Anagrafica) from TabClienti the field contains bytes instead of if i try to select * from TabClienti the field will be empty, what can do for export data in LATIN or ASCII format?

1 Reply

Good day <?!?>

 

If I understand your request correctly, then you want to CONVERT NVARCHAR into VARCHAR. This means that you might lose data, since Unicode includes 1,114,112 code positions and maps over 100k different characters while extended ASCII only include 256 code positions, which mean it cannot map more then 256 different characters!

 

Once you took this into consideration you might find the following trick helpful

/*********************************************************  */
/**************************** CONVERT FROM Unicode to Ascii */
/*********************************************************  */
--------------------------------------- Demo table (DDL+DML)
DROP TABLE IF EXISTS T
GO
create table T(txt nvarchar(100)) -- Unicode
GO
INSERT T(txt) VALUES (N'רונן ronen') -- Hebrew & English Characters (my First name)
GO
SELECT * FROM T
GO

--------------------------------------- Solution Using temp table
DROP TABLE IF EXISTS #T;
CREATE TABLE #T (txt VARCHAR(200) COLLATE Hebrew_CI_AI)
--INSERT T_NEW (txt) SELECT CONVERT (VARCHAR(200), txt COLLATE Hebrew_CI_AI)
INSERT #T (txt) SELECT txt
FROM T

SELECT* FROM T
SELECT* FROM #T
GO