DISTINCT in a SQL statements with a TEXT field

  • DISTINCT in a SQL statements with a TEXT field bombs.

    Is there any work arounds out there?

  • Can you provide us with an example so that we can see what work-arounds might be applicable?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • OK, I think I know what this one is...

    Try this:

    Select distinct CAST(TextCol as Varchar(MAX))

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Actually the code is -- select distinct * into table_remove_dups from table_A

    so I have several fields and want to make sure all are unique with out duplicate values but one of my fields is a text field. Other than your suggestion to recast the data type (which is simular to me simply changing the datatype which I will do if there is no other way to do it) is there another way to check for the distinctness of this field with the others together? I don't really want to change the data type because I have chosen this data type as I believe that it is the largest character/string data type available to me and this field may become quite large.

  • This is the solution that you want to use. VARCHAR(MAX) can be as large as any TEXT datatype and is superior in virtually every respect. You can add all of the other columns onto this statement:

    [Code]

    Select distinct Col1Name

    , Col2Name

    ...

    , CAST(TextColName as Varchar(MAX)) as [TextColName]

    , ...

    into table_remove_dups

    from table_A

    [/code]

    If you really want to keep the TEXT in the output (though I strongly encourage you to move to Varchar(MAX)), this this should work:

    [Code]

    Select Col1Name

    , Col2Name

    ...

    , CAST(TextColName as TEXT) as [TextColName]

    , ...

    into table_remove_dups

    from (Select distinct Col1Name

    , Col2Name

    ...

    , CAST(TextColName as Varchar(MAX)) as [TextColName]

    , ...

    from table_A) as A

    [/code]

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanks very much -- I didn't realize the Varchar(max) was the same size as Text column this in itself resolves my issue.

  • Thanks for the feedback, Mark.

    Varchar(MAX) is one of the best things about SQL Server 2005, completely gets rid of the need for the BLOB's (Text, NText, etc.), meaning no more messy special functions and commands just to manipulate TEXT columns, no more mysterious failures that turn out to be because TEXT is not supported in that context (replication, etc.).

    IMHO, this alone is reason enough to upgrade to SQL 2005.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply