You either need to repeat the SUBSTRING calculations in the CASE, use a derived table or CTE. And don't put single quotes around the alias names when referencing them, otherwise they are treated as a string rather than the alias name.Here's the derived table:select *, CASE WHEN BAAN_INVOICE = BAAN_SOLD OR BAAN_INVOICE = BAAN_SHIP OR BAAN_SOLD = BAAN_SHIP THEN 1 ELSE 0END as 'Rating'from ( select a.id,a.name ,s.External_Unique_Key__c as 'Loc_Key' ,SUBSTRING (s.External_Unique_Key__c,1,3) as 'BAAN_ERP' ,SUBSTRING (s.External_Unique_Key__c,5,9) as 'BAAN_INVOICE' ,SUBSTRING (s.External_Unique_Key__c,15,9) as 'BAAN_SOLD' ,SUBSTRING (s.External_Unique_Key__c,25,9) as 'BAAN_SHIP' from salesforce...[SVMXC__Site__c] s (nolock) --join salesforce...[ERP_Company_Configuration__c] e (nolock)on e.id = s.ERP_Company_Number1__c join qa3salesforce...[account] a (nolock) on a.id = s.SVMXC__Account__c and a.recordtypeid ='012300000000XGBAA2'-- QA3A.LSGNASD --left join qa3salesforce...[Financial_Parent__c] f (nolock) on f.id= s.ERP_Parent_Account_Number__c and f.recordtypeid = '01230000000ZP55AAG') t
Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/