在SQL Server 2012中将varbinary()转换为varchar(max)时,如何编码语言特定的字符? [英] How to encode language specific chars while converting varbinary() to varchar(max) in SQL Server 2012?

查看:268
本文介绍了在SQL Server 2012中将varbinary()转换为varchar(max)时,如何编码语言特定的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据库列 DATA varbinary()转换为 varchar (最大)在SQL Server 2012中。



我正在使用此代码来处理转换:

  SELECT CONVERT(VARCHAR(MAX),DATA)FROM [dbo]。[TABLE_NAME] 

,结果行如下:

  VW 6501Çamaşır 

我对语言特定的字符有困难(现在我的情况下是土耳其语)



如何解决SQL Server 2012中的编码问题?



是否有通用的方法来进行此转换任何语言,考虑到任何给定语言的数据/编码问题的丢失?



这可能听起来像一个菜鸟问题,但我真的很感激任何建议或答案。 >

谢谢,

解决方案

一般来说,SQL Server不会ld UTF-8高度重视。
然而,.NET有方法可以执行此操作,您可以通过CLR集成获取它们。



使用C#编译它:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

使用System.Data.SqlTypes;
使用Microsoft.SqlServer.Server;

命名空间UtfLib
{
public static class UtfMethods
{
[SqlFunction(IsDeterministic = true,IsPrecise = true)]
public static SqlBinary NVarCharToUtf8(SqlString inputText)
{
if(inputText.IsNull)
返回新的SqlBinary(); //(null)

返回新的SqlBinary(Encoding.UTF8.GetBytes(inputText.Value));
}

[SqlFunction(IsDeterministic = true,IsPrecise = true)]
public static SqlString Utf8ToNVarChar(SqlBinary inputBytes)
{
if(inputBytes。 IsNull)
返回新的SqlString(); //(null)

返回新的SqlString(Encoding.UTF8.GetString(inputBytes.Value));
}
}
}

将程序集导入数据库并创建外部函数:

  CREATE ASSEMBLY UtfLib 
FROM'C:\UtfLib.dll'
GO
创建功能NVarCharToUtf8(@InputText NVARCHAR(MAX))
RETURNS VARBINARY(MAX)
作为外部名称UtfLib。[UtfLib.UtfMethods] .NVarCharToUtf8
GO
创建功能Utf8ToNVarChar(@InputBytes VARBINARY(MAX))
RETURNS NVARCHAR(MAX)
作为外部名称UtfLib。[UtfLib.UtfMethods] .Utf8ToNVarChar
pre>

最后一步,您必须启用clr

  sp_configure' clr enabled',1 
GO
RECONFIGURE
GO
sp_configure'clr enabled' - 确保花费
GO

和voilà!

  SELECT dbo.Utf8ToNVarChar (DATA)FROM [dbo]。[TABLE_NAME] 


I am trying to convert a database column DATA from varbinary() to varchar(max) in SQL Server 2012.

I am using this code to handle the conversion:

SELECT CONVERT(VARCHAR(MAX), DATA) FROM [dbo].[TABLE_NAME]

and the resulting row is as follows :

VW 6501 Çamaşır

I am having trouble with language specific characters (language is Turkish in my case for now)

How do I get over this encoding problem in SQL Server 2012?

Is there a generic way to do this conversion for any language, considering loss of data/encoding problems for any given language?

This may sound like a rookie question but I really would appreciate any suggestions or answer.

Thank you,

解决方案

In general, SQL Server does not hold UTF-8 in high regard. However, .NET has methods to do this and you can get at them via CLR integration.

Compile this using C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

namespace UtfLib
{
    public static class UtfMethods
    {
        [SqlFunction(IsDeterministic = true, IsPrecise = true)]
        public static SqlBinary NVarCharToUtf8(SqlString inputText)
        {
            if (inputText.IsNull)
                return new SqlBinary(); // (null)

            return new SqlBinary(Encoding.UTF8.GetBytes(inputText.Value));
        }

        [SqlFunction(IsDeterministic = true, IsPrecise = true)]
        public static SqlString Utf8ToNVarChar(SqlBinary inputBytes)
        {
            if (inputBytes.IsNull)
                return new SqlString(); // (null)

            return new SqlString(Encoding.UTF8.GetString(inputBytes.Value));
        }
    }
}

Import the assembly into your database and create the external functions:

CREATE ASSEMBLY UtfLib
FROM 'C:\UtfLib.dll'
GO
CREATE FUNCTION NVarCharToUtf8 (@InputText NVARCHAR(MAX))
RETURNS VARBINARY(MAX)
AS EXTERNAL NAME UtfLib.[UtfLib.UtfMethods].NVarCharToUtf8
GO
CREATE FUNCTION Utf8ToNVarChar (@InputBytes VARBINARY(MAX))
RETURNS NVARCHAR(MAX)
AS EXTERNAL NAME UtfLib.[UtfLib.UtfMethods].Utf8ToNVarChar

Last step, you have to enable clr

sp_configure 'clr enabled',1
GO
RECONFIGURE
GO
sp_configure 'clr enabled'  -- make sure it took
GO

and voilà!

SELECT dbo.Utf8ToNVarChar(DATA) FROM [dbo].[TABLE_NAME]

这篇关于在SQL Server 2012中将varbinary()转换为varchar(max)时,如何编码语言特定的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆