SQL查询整理麻烦

时间:2014-01-15 12:01:44

标签: sql-server tsql collation

我有一个像

这样的选择查询
SELECT
  itemCode,
  itemDefinition,
  clientCode,
  clientDefinition
FROM
  Invoices 

数据库中的发票表具有归类SQL_Latin1_General_CP1_CI_AS

问题是,当我在程序中收到数据时,itemDefinition列和clientDefinition中的土耳其语字符似乎不正确。例如土耳其字母Ğ成为其他角色,Ş别的等等。

有人可以帮我解决这个问题吗?

由于

enter image description here

1 个答案:

答案 0 :(得分:-1)

解决方案是这样的:首先,需要为字符转换创建函数,如下所示:

CREATE FUNCTION [dbo].[ChangetoTRK](@A NVARCHAR(4000))    
RETURNS NVARCHAR(4000)      
AS    
BEGIN   
  DECLARE @TMP NVARCHAR(4000)     
  SET @TMP = REPLACE(@A COLLATE LATIN1_GENERAL_BIN, CHAR(208), N'Ğ')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(220), N'Ü')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(222), N'Ş')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(221), N'İ')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(214), N'Ö')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(199), N'Ç')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(73), N'I')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(240), N'ğ')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(252), N'ü')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(254), N'ş')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(105), N'i')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(246), N'ö')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(231), N'ç')
  SET @TMP = REPLACE(@TMP COLLATE LATIN1_GENERAL_BIN, CHAR(253), N'ı')
  RETURN @TMP
END

然后,我们在查询中使用它:

SELECT
  itemCode,
  dbo.ChangeToTRK(itemDefinition),
  clientCode,
  dbo.ChangeToTRK(clientDefinition)

这       发票