Search code examples
sqlsql-servertype-conversionfreetextfreetexttable

SQL Server Freetext Varbinary returns nothing and Conversion VARBINARY -> VARCHAR Not working


Hey I've a few issues with my solution and I can't figure it out

Below is my creation table

    CREATE TABLE FreeTextSearch
(
    [ID] BIGINT NOT NULL IDENTITY(1,1),
    [Content] VARBINARY(MAX) NOT NULL,
    [SubjectClass] VARCHAR(30) NOT NULL,
    [SubjectID] VARCHAR(30) NOT NULL,
    [ColumnName] VARCHAR(128) NOT NULL,
    CONSTRAINT PK_FreeTextSearch PRIMARY KEY (ID)
);


ALTER TABLE FreeTextSearch
Add FileExtension As '.html';

CREATE INDEX FreeTextSearch_SubjectClass
ON FreeTextSearch ([SubjectClass]); 


CREATE FULLTEXT CATALOG [freetext_catalog]


CREATE FULLTEXT INDEX ON FreeTextSearch
(
    [Content] TYPE COLUMN FileExtension
)
    KEY INDEX PK_FreeTextSearch ON freetext_catalog;

This will create a FreeTextSearch table, with FileExtension of HTML etc.

I've the data converted to VARBINARY automatically inserted into the table based on a trigger but there are two issues right now

Content like:

<p>Test note for free text bla!</p>

Won't be detected by query like

    SELECT
    Id,
    CONVERT(VARCHAR(MAX), [Content]) AS [Content]
FROM dbo.FreeTextSearch
WHERE FREETEXT ([Content], 'Test')

Additionally

    SELECT
    Id,
    CONVERT(VARCHAR(MAX), [Content]) AS [Content]
FROM dbo.FreeTextSearch

Will return

ID: 1 Content <
ID: 2 Content <

While

    SELECT
    Id,
    CONVERT(XML, [Content]) AS [Content]
FROM dbo.FreeTextSearch

Will Return

ID: 1 Content: <p>Test note for free text bla!</p>
ID: 2 Content: <p>Something is very wrong with this one i think</p><p>Not really sure what.</p>

Which is the correct data but it's unclear why

  1. FREETEXT doesn't find anything
  2. Why VARCHAR(MAX) returns only <

Solution

  • select test, cast(test as varchar(max)), cast(test as nvarchar(max))
    from
    (
    select cast('a' as varbinary(max)) + 0x0 /* nul in ascii, anything after that not printed*/ + cast('123' as varbinary(max)) as test
    ) as src;
    
    
    select test, cast(test as varchar(max)), cast(test as nvarchar(max))
    from
    (
    select cast(N'<p>Test note for free text bla!</p>' as varbinary(max)) as test
    ) as src;