Convert image datatype into varchar in sql server 2008

前端 未结 5 1901
后悔当初
后悔当初 2021-02-05 07:18

We have TestPartner database in SQL Server. The descriptions of the bugs are stored in \"image\" datatype column. We need to write a query to display the data as html table. We

5条回答
  •  野的像风
    2021-02-05 07:26

    If the data has been stored in the image field as Unicode data, it won't work if you replace the line Select CONVERT(nvarchar(1000), convert(varbinary(1000), tpcommanddetail)) From TestPartnerDB.TP_RESULTS_RECORDS with Select CONVERT(varchar(1000), convert(varbinary(1000), tpcommanddetail)) From TestPartnerDB.TP_RESULTS_RECORDS.

    It's very important that the first conversion from binary data to text be made with the right collation and size of characters: if the text is in Ascii, you must use varchar() and if the text is in Unicode, you must use nvarchar().

    The second conversion from nvarchar(100) to varchar(100) looks useless to me.

    The use of binary(100) instead of varbinary(100) looks also very suspicious to me.

    Finally, if you get strange characters like 0x0002 then maybe this is why that this has been stored in an image field instead of a text field: this is a specially formated field where not all characters are text characters. However, as you didn't show us the result of printing the original field in binary (or more exactly, in hexadecimal) or of any of your results, it's impossible to say anything more.

    I've just cooked up a few tests; with these, you should be able to understand what's happening:

    select ascii ('A'), unicode(N'A');
    select convert (binary(2), ascii('A')), convert (binary(2), unicode(N'A'));
    --
    declare @ab binary(10), @vab varbinary(10);
    declare @nab binary(10), @vnab varbinary(10);
    --
    set @ab = convert (binary (10), 'AB');
    set @vab = convert (varbinary (10), 'AB');
    set @nab = convert (binary (10), N'AB');
    set @vnab = convert (varbinary (10), N'AB');
    --
    select @ab, @vab, @nab, @vnab;
    --
    select convert(varchar(10), @ab) + '*', 
        convert(varchar(10), @vab) + '*', 
        convert(varchar(10), @nab) + '*', 
        convert(varchar(10), @vnab) + '*';
    --
    select len(convert(varchar(10), @ab)), 
        len(convert(varchar(10), @vab)), 
        len(convert(varchar(10), @nab)), 
        len(convert(varchar(10), @vnab));
    --
    select len(convert(varchar(10), @ab) + '*'), 
        len(convert(varchar(10), @vab) + '*'), 
        len(convert(varchar(10), @nab) + '*'), 
        len(convert(varchar(10), @vnab) + '*');
    --
    select convert(nvarchar(10), @ab) + '*', 
        convert(nvarchar(10), @vab) + '*', 
        convert(nvarchar(10), @nab) + '*', 
        convert(nvarchar(10), @vnab) + '*';
    --
    select len(convert(nvarchar(10), @ab)), 
        len(convert(nvarchar(10), @vab)), 
        len(convert(nvarchar(10), @nab)), 
        len(convert(nvarchar(10), @vnab));
    --
    select convert(varchar(10), convert(nvarchar(10), @ab)) + '*', 
        convert(varchar(10), convert(nvarchar(10), @vab)) + '*', 
        convert(varchar(10), convert(nvarchar(10), @nab)) + '*', 
        convert(varchar(10), convert(nvarchar(10), @vnab)) + '*';
    --
    select len(convert(varchar(10), convert(nvarchar(10), @ab))), 
        len(convert(varchar(10), convert(nvarchar(10), @vab))), 
        len(convert(varchar(10), convert(nvarchar(10), @nab))), 
        len(convert(varchar(10), convert(nvarchar(10), @vnab)));
    --
    select convert(nvarchar(10), @nab) for xml path('tr');
    select convert(varchar(10), convert(nvarchar(10), @nab)) for xml path('tr');
    select 'The Name' as td, '', convert(nvarchar(10), @nab) as td for xml path('tr');
    

提交回复
热议问题