Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

DISTINCT in a SQL statements with a TEXT field Expand / Collapse
Author
Message
Posted Tuesday, August 12, 2008 9:40 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 23, 2014 12:32 PM
Points: 177, Visits: 550
DISTINCT in a SQL statements with a TEXT field bombs.
Is there any work arounds out there?

Post #551140
Posted Tuesday, August 12, 2008 9:48 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 1:19 PM
Points: 9,294, Visits: 9,491
Can you provide us with an example so that we can see what work-arounds might be applicable?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #551145
Posted Tuesday, August 12, 2008 9:50 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 1:19 PM
Points: 9,294, Visits: 9,491
OK, I think I know what this one is...

Try this:

Select distinct CAST(TextCol as Varchar(MAX))



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #551153
Posted Tuesday, August 12, 2008 10:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 23, 2014 12:32 PM
Points: 177, Visits: 550
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.
Post #551208
Posted Tuesday, August 12, 2008 10:38 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 1:19 PM
Points: 9,294, Visits: 9,491
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:
Select distinct Col1Name
, Col2Name
...
, CAST(TextColName as Varchar(MAX)) as [TextColName]
, ...
into table_remove_dups
from table_A

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:
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



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #551220
Posted Tuesday, August 12, 2008 10:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 23, 2014 12:32 PM
Points: 177, Visits: 550
Thanks very much -- I didn't realize the Varchar(max) was the same size as Text column this in itself resolves my issue.
Post #551227
Posted Tuesday, August 12, 2008 11:25 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 1:19 PM
Points: 9,294, Visits: 9,491
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.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #551263
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse