我在sql server数据库中有一个unicode列nvarchar。 我在本地化的“Indian Language GUJARATI”中将数值存储在该列中。 如何在nvarchar列上使用sum,avg等聚合函数。
注意:该nvarchar列包含本地化语言的unicode数据。
答案 0 :(得分:1)
我在sql server和.net中都有以下程序。
将英语转换为古吉拉特语Unicode和 将古吉拉特语Unicode转换为英语数字
public static class global
{
public static string Translate(string source, string fromStr, string toStr)
{
string result = "";
foreach (var sourceChar in source)
{
int pos = fromStr.IndexOf(sourceChar);
if ((pos >= 0) && (pos < toStr.Length))
{
result += toStr[pos];
}
}
return result;
}
public static string ToGujDig(this object source)
{
string fromStr = ".0123456789";
string toStr = ".\u0AE6\u0AE7\u0AE8\u0AE9\u0AEA\u0AEB\u0AEC\u0AED\u0AEE\u0AEF\u0AF0";
string result = "";
string s = source.ToString();
foreach (var sourceChar in s)
{
int pos = fromStr.IndexOf(sourceChar);
if ((pos >= 0) && (pos < toStr.Length))
{
result += toStr[pos];
}
}
return result;
}
public static string ToEngDig(this string source)
{
string fromStr = ".\u0AE6\u0AE7\u0AE8\u0AE9\u0AEA\u0AEB\u0AEC\u0AED\u0AEE\u0AEF\u0AF0";
string toStr = ".0123456789";
string result = "";
string s = source.ToString();
foreach (var sourceChar in s)
{
int pos = fromStr.IndexOf(sourceChar);
if ((pos >= 0) && (pos < toStr.Length))
{
result += toStr[pos];
}
}
return result;
}
}
传递数值并获取unicode值
CREATE FUNCTION [dbo].[NumericToUnicode]
(
@String VARCHAR(50)
)
RETURNS NVARCHAR(50)
BEGIN
DECLARE @rString NVARCHAR(50)
set @rString= N''
DECLARE @length INT
set @length = LEN(@String)
DECLARE @position INT
set @position= 1
WHILE @position <= @length
BEGIN
IF (ASCII(SUBSTRING(@String, @position, 1))) = 46
SET @rString = @rString + N'.'
ELSE
SET @rString = @rString + NCHAR(ASCII(SUBSTRING(@String, @position, 1)) + 2742)
SET @position = @position + 1
END
RETURN @rString
END
-- Test it
SELECT [dbo].[NumericToUnicode]('50.0') or
SELECT [dbo].[NumericToUnicode](50.12)
传递unicode值并获取数字值
Create FUNCTION [dbo].[UnicodeToNumeric]
(
@nString NVARCHAR(50)
)
RETURNS NUMERIC(12,2)
BEGIN
DECLARE @rString NVARCHAR(50)
set @rString=''
DECLARE @position INT
set @position=1
WHILE @position <= LEN(@nString)
BEGIN
IF (UNICODE(SUBSTRING(@nString, @position, 1))) = 46
SET @rString = @rString + CHAR(UNICODE(SUBSTRING(@nString, @position, 1)))
ELSE
SET @rString = @rString + CHAR(UNICODE(SUBSTRING(@nString, @position, 1)) - 2742)
SET @position = @position + 1
END
RETURN @rString
END
希望这有帮助。