2

I am trying to get some SQL to execute but am getting the following error

    Msg 451, Level 16, State 1, Line 1
Cannot resolve collation conflict for column 4 in SELECT statement.

But cannot seem to figure out where the problem is??? Any help would be very much appreciated.

SELECT     MEMBTYPEID.text AS MemberType, MEMBLST.nodeId, MEMBTYPES.Name AS MemberField, 
                      ISNULL(CASE WHEN MEMBTYPES.datatypeID IN
                          (SELECT     NodeId
                            FROM          DBO.CMSDATATYPE
                            WHERE      DBTYPE = 'Nvarchar') THEN MEMBDATA.[dataNvarchar] WHEN MEMBTYPES.datatypeID IN
                          (SELECT     NodeId
                            FROM          DBO.CMSDATATYPE
                            WHERE      DBTYPE = 'Ntext') THEN MEMBDATA.[dataNtext] WHEN MEMBTYPES.datatypeID IN
                          (SELECT     NodeId
                            FROM          DBO.CMSDATATYPE
                            WHERE      DBTYPE = 'Date') THEN CONVERT(NVARCHAR, MEMBDATA.[dataDate]) WHEN MEMBTYPES.datatypeID IN
                          (SELECT     NodeId
                            FROM          DBO.CMSDATATYPE
                            WHERE      DBTYPE = 'Integer') THEN CASE WHEN
                          (SELECT     value
                            FROM          [dbo].[cmsDataTypePreValues]
                            WHERE      datatypenodeid = MEMBTYPES.[dataTypeId] AND id = CONVERT(INT, MEMBDATA.[dataInt])) IS NOT NULL THEN
                          (SELECT     value
                            FROM          [dbo].[cmsDataTypePreValues]
                            WHERE      datatypenodeid = MEMBTYPES.[dataTypeId] AND id = CONVERT(INT, MEMBDATA.[dataInt])) ELSE CONVERT(NVARCHAR, 
                      MEMBDATA.[dataInt]) END ELSE NULL END, '') AS MemberData
FROM         (SELECT     id, text
                       FROM          dbo.umbracoNode
                       WHERE      (nodeObjectType = '9b5416fb-e72f-45a9-a07b-5a9a2709ce43')) AS MEMBTYPEID LEFT OUTER JOIN
                          (SELECT     nodeId, contentType
                            FROM          dbo.cmsContent) AS MEMBLST ON MEMBLST.contentType = MEMBTYPEID.id LEFT OUTER JOIN
                      dbo.cmsPropertyType AS MEMBTYPES ON MEMBTYPES.contentTypeId = MEMBLST.contentType LEFT OUTER JOIN
                      dbo.cmsPropertyData AS MEMBDATA ON MEMBDATA.contentNodeId = MEMBLST.nodeId AND 
                      MEMBDATA.propertytypeid = MEMBTYPES.id LEFT OUTER JOIN
                      dbo.cmsMember AS MEMB ON MEMB.nodeId = MEMBLST.nodeId
WHERE     (MEMBLST.nodeId IS NOT NULL)

My SQL skills are fairly basic, so hope someone can help

~~~~~~~~~~~~ WORKING CODE ~~~~~~~~~~~~~

Managed to get it working and here is the code

SELECT MEMBTYPEID.text AS MemberType, MEMBLST.nodeId, MEMBTYPES.Name AS MemberField, MEMBTYPES.Alias AS MemberFieldAlias, MEMB.LoginName,
ISNULL(CASE 
WHEN MEMBTYPES.datatypeID IN (SELECT NodeId FROM DBO.CMSDATATYPE WHERE DBTYPE = 'Nvarchar') THEN MEMBDATA.[dataNvarchar] 
WHEN MEMBTYPES.datatypeID IN (SELECT NodeId FROM DBO.CMSDATATYPE WHERE DBTYPE = 'Ntext') THEN MEMBDATA.[dataNtext] 
WHEN MEMBTYPES.datatypeID IN (SELECT NodeId FROM DBO.CMSDATATYPE WHERE DBTYPE = 'Date') THEN CONVERT(NVARCHAR, MEMBDATA.[dataDate]) 
WHEN MEMBTYPES.datatypeID IN (SELECT NodeId FROM DBO.CMSDATATYPE WHERE DBTYPE = 'Integer') THEN  CONVERT(NVARCHAR, MEMBDATA.[dataInt])
ELSE NULL END, NULL) 
AS MemberData 
FROM 
(SELECT id, text FROM dbo.umbracoNode WHERE (nodeObjectType = '9b5416fb-e72f-45a9-a07b-5a9a2709ce43')) AS MEMBTYPEID 
LEFT OUTER JOIN (SELECT nodeId, contentType FROM dbo.cmsContent) AS MEMBLST ON MEMBLST.contentType = MEMBTYPEID.id 
LEFT OUTER JOIN dbo.cmsPropertyType AS MEMBTYPES ON MEMBTYPES.contentTypeId = MEMBLST.contentType 
LEFT OUTER JOIN dbo.cmsPropertyData AS MEMBDATA ON MEMBDATA.contentNodeId = MEMBLST.nodeId AND MEMBDATA.propertytypeid = MEMBTYPES.id 
LEFT OUTER JOIN dbo.cmsMember AS MEMB ON MEMB.nodeId = MEMBLST.nodeId
WHERE (MEMBLST.nodeId IS NOT NULL)
1
  • Thanks everyone for the help I got it working in the end, and have edited the above to add the working version
    – YodasMyDad
    Commented Feb 17, 2011 at 13:07

4 Answers 4

9

A collation is basically a codepage that tells sql how to interpret/compare/sort strings . It can be for example be case (in)sensitive or (not) ignore accents (like ^ in French). Fore more info, see here.

In sql server, you set the collation on the server/database/column level where each lower level can override the default of the higher level. That makes it possible that you will compare strings from two different collations. And that's where the catch. Sometimes it's impossible to compare 2 different collations. For example, if column 1 has a case insensitive collation and column 2 a case sensitive and you compare 'AAA' from column 1 with 'aaa' from column 2, are they equal or not ? In such a case, sql throws a collation error.

column 4 refers to your MemberData column i.e. the giant ISNULL(Case ... statement. Sql collation conflicts typically happen with compares between strings so that means one of your IN or = operators is the culprit. To debug, I would gradually remove parts of that statement until the error doesn't happen anymore. Then check the collation of the columns in the part you just removed. If they are different, it's very likely that's your problem.

You then could use COLLATE to force the string of one of those columns to cast to the collation of the other column. Be warned however that depending on your collation you can get weird compare results i.e. 'Â' can be equal to 'a' or not. I would try to pin down which strings in your columns are giving the collation error so that you can see what would be the effect from using COLLATE.

As a general rule, I would recommend to never override the default collation of your database just to prevent this sort of headaches.

2

For the fourth column, I suspect that something either side of the '=' is a different collation. You could try putting 'collate database_default' either side of the '='. If you try 'SP_HELP' on each of the table names used by the fourth column, this will help you to see if there are any differences in the collation at the column level. It's not the type of data that's the issue, it's the type of collation of the column. As SemVanmeenen has said above, for the query affecting the 4th column, try to remove some of the parts of the statement and try to run it, and then put them in again. That might help you to identify the offending line of code. Good luck! Please let us know how you get on. Regards, Jen Stirrup www.jenstirrup.com

0

Do you have "collate" columns in your tables ?

If true you should add "collate" after their names. As an exemple if cmsPropertyType.contentTypeId is a collate column. Instead of : ON MEMBTYPES.contentTypeId = MEMBLST.contentType

You should write : ON MEMBTYPES.contentTypeId collate 'contentTypeIdCollateAlias' = MEMBLST.contentType

Check out documentation : Doc on MSDN

And read the SemVanmeen's post for a better understanding of your probleme

0

Two suspects.

First one is this:

SELECT     value
FROM          [dbo].[cmsDataTypePreValues]
WHERE      datatypenodeid = MEMBTYPES.[dataTypeId] AND id = CONVERT(INT, MEMBDATA.[dataInt])

What type is value? It should probably be nvarchar, otherwise you should convert it accordingly.

Second one is the default '' in the ISNULL. Maybe you should define it as N''?

Also, regarding the subselect, why does it have to be repeated twice, in WHEN, and afterwards in THEN? You could replace that sub-CASE completely with COALESCE((SELECT value...), CONVERT(NVARCHAR, MEMBDATA.[dataInt])).

Not the answer you're looking for? Browse other questions tagged or ask your own question.