SQL Server - Convert varchar to another collation (code page) to fix character encoding

前端 未结 4 1690
自闭症患者
自闭症患者 2021-01-01 12:46

I\'m querying a SQL Server database that uses the SQL_Latin1_General_CP850_BIN2 collation. One of the table rows has a varchar with a value that includes the +/- character

4条回答
  •  伪装坚强ぢ
    2021-01-01 13:21

    We may need more information. Here is what I did to reproduce on SQL Server 2008:

    CREATE DATABASE [Test] ON  PRIMARY 
        ( 
        NAME = N'Test'
        , FILENAME = N'...Test.mdf' 
        , SIZE = 3072KB 
        , FILEGROWTH = 1024KB 
        )
        LOG ON 
        ( 
        NAME = N'Test_log'
        , FILENAME = N'...Test_log.ldf' 
        , SIZE = 1024KB 
        , FILEGROWTH = 10%
        )
        COLLATE SQL_Latin1_General_CP850_BIN2
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[MyTable]
        (
        [SomeCol] [varchar](50) NULL
        ) ON [PRIMARY]
    GO
    Insert MyTable( SomeCol )
    Select '±' Collate SQL_Latin1_General_CP1_CI_AS
    GO
    Select SomeCol, SomeCol Collate SQL_Latin1_General_CP1_CI_AS
    From MyTable
    

    Results show the original character. Declaring collation in the query should return the proper character from SQL Server's perspective however it may be the case that the presentation layer is then converting to something yet different like UTF-8.

提交回复
热议问题