?
Solved

SQL DISTINCT ON TEXT FIELD CAUSES ERROR

Posted on 2001-06-20
4
Medium Priority
?
2,003 Views
Last Modified: 2008-03-04
I have a query that is returning dups.  I tried using DISTINCT but it wont allow it because I do a select * and some of the fields are TEXT fields.  How can I work around this?  I have to have those in my query Thanks.
0
Comment
Question by:bostonroxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:arun04
ID: 6213303
Use convert function on columns of TEXT type and convert them to VARCHAR, it will truncate some data but it works fine
0
 

Accepted Solution

by:
meetyg earned 150 total points
ID: 6214254
The Distinct feature doesn't work on TEXT,IMAGE, NTEXT or BINARY data columns.
If the text isn't more than 8kb of data (on SQL 2000, or 255 bytes if your using SQL 6.5 or 7), you can convert as mentioned. If the text field is bigger than that, data maybe lost, or it moght not work at all.

Heres how to do the convert:
------------------------------------------------------
select Distinct Convert(text_column_name,varchar(8000)) from mytable_tbl
------------------------------------------------------

notice that I chose to convert to varchar(8000) because 8000 bytes is the maximum amount of data that varchar can hold in SQL server 2000.

If i'm not mistaken, SQL server 6.5 and 7 can only handle 255 bytes of data in the varchar datatype.

Either way, you cannot explicitly use distinct on TEXT columns.

Goodluck.

0
 

Author Comment

by:bostonroxx
ID: 6214569
Should I just change this to Varchar(2000)?
0
 

Expert Comment

by:meetyg
ID: 6218835
Yes, if 2000 characters is enough, you can convert to varchar(2000)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question