Question : SQL Query Help needed - The text, ntext, or image data type cannot be selected as DISTINCT.

I know where its catching, but not to sure how to work around it.

Server: Msg 8163, Level 16, State 3, Line 2
The text, ntext, or image data type cannot be selected as DISTINCT.

It seems to not like the following in the Order by.

CASE pc.Description
      WHEN '**long**' THEN pc.DescriptionLong
      ELSE pc.Description
      END,
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
SET NOCOUNT ON
SELECT	DISTINCT
	dbo.FormatName(pp.Prefix,pp.First,pp.Middle, pp.Last, pp.Suffix) AS Name,
	pv.TicketNumber AS [Ticket Number],
	pva.InsBalance, 
	pva.PatBalance,  
	pva.InsBalance + pva.PatBalance AS [Total Balance],  
	ISNULL(ic.ListName, '') AS [Insurance Carrier], 
	ISNULL(ig.name,'No Ins Group') as InsGroupName,
	pv.Visit AS [Visit Date / Time], 
	pc.Created AS [Date Created],
	pc.CreatedBy AS [Created By], 
	CASE pc.Description
		WHEN '**long**' THEN pc.DescriptionLong
		ELSE pc.Description
		END AS Notes,
	df.ListName AS Doctor,
	fac.ListName AS Facility,
	ISNULL(pp.ProfileNotes,'None') AS [Patient Notes],
	ISNULL(pp.BillingNotes, 'None') AS [Billing Notes]
 
FROM	PatientProfile pp
	INNER JOIN PatientVisit pv ON pp.PatientProfileId = pv.PatientProfileId
	INNER JOIN PatientCorrespondence pc ON pp.PatientProfileId = pc.PatientProfileId AND pv.PatientVisitId = pc.PatientVisitId
	INNER JOIN DoctorFacility df ON pv.DoctorId = df.DoctorFacilityId
	LEFT JOIN InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersId = ic.InsuranceCarriersId 
	LEFT JOIN InsuranceGroup ig on ic.InsuranceGroupID = ig.InsuranceGroupID
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 
	INNER JOIN PatientVisitAgg pva ON pv.PatientVisitId = pva.PatientVisitId 
	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId 
 
WHERE  pva.InsBalance > 0 AND 
	(
	(NULL IS NOT NULL AND pv.PatientProfileId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Doctor 
	(
	(NULL IS NOT NULL AND pv.DoctorId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityId IN (NULL)) OR
	(NULL IS NULL)
	)
        AND  --Filter on user
        (
	(NULL IS NOT NULL AND pc.CreatedBy LIKE '%NULL%') OR
        (NULL IS NULL)
        )
	AND  --Filter on Insurance Carrier
	(
	(NULL IS NOT NULL AND pv.PrimaryInsuranceCarriersId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --Filter on Ins Group
	(
	(NULL IS NOT NULL AND ic.InsuranceGroupID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Date
	(
	(1 = 1 AND (pv.Entered >= ISNULL(NULL, '1/1/1900') AND pv.Entered < DATEADD(d,1,ISNULL(NULL,'1/1/3000'))))                 OR
	(1 = 2 AND (pvp.DateOfServiceFrom>= ISNULL(NULL, '1/1/1900') AND pvp.DateOfServiceFrom <                                                        		DATEADD(d,1,ISNULL(NULL,'1/1/3000'))))
	)
	AND
	(pc.Created >= ISNULL(NULL,'1/1/1900') AND pc.Created < DATEADD(d, 1, ISNULL(NULL,'1/1/3000')))
 
ORDER BY 
	dbo.FormatName(pp.Prefix,pp.First,pp.Middle, pp.Last, pp.Suffix),
	pv.TicketNumber,
	pva.InsBalance, 
	pva.PatBalance,  
	pva.InsBalance + pva.PatBalance,  
	ISNULL(ic.ListName, ''), 
	ISNULL(ig.name,'No Ins Group'),
	pv.Visit, 
	pc.Created,
	pc.CreatedBy, 
	CASE pc.Description
		WHEN '**long**' THEN pc.DescriptionLong
		ELSE pc.Description
		END,
	df.ListName,
	fac.ListName,
	ISNULL(pp.ProfileNotes,'None'),
	ISNULL(pp.BillingNotes, 'None')
Open in New Window Select All

Answer : SQL Query Help needed - The text, ntext, or image data type cannot be selected as DISTINCT.

Try:
CAST(pc.DescriptionLong  as VARCHAR(8000))

I've run into this problem a few times myself... hopefully your text field will never exceed 8000 chars, so you will never run into any problems....

There are some ways to get around it but I don't believe that ithey are very efficient....
Random Solutions  
 
programming4us programming4us