-2

I am trying to execute the below query.

SELECT
ABL.Address1 AS BillingAddress1,
ABL.Address2 AS BillingAddress2,
ABL.Address3 AS BillingAddress3,
ABL.Address4 AS BillingAddress4,
ABL.UnitNumber AS BillingUnitNumber,
ABL.City AS BillingCity,
ABL.ProvinceOrState AS BillingProvinceOrState,
ABL.PostalCode AS BillingPostalCode,
CASE ISNULL (ABL.CountryCode,'')
                                WHEN 'CAN' THEN 'CANADA'
                                WHEN 'CA' THEN 'CANADA'
                                WHEN 'US' THEN 'USA'    
                                WHEN '' THEN
CASE WHEN CHARINDEX(RTRIM(ABL.ProvinceOrState),'BC,NWT,SK,MB,ON,QC,NB,NS,PEI,NF')>0                                                                                THEN 'CANADA'                                                                    ELSE NULL                                                                              END
ELSE ABL.CountryCode                
END AS BillingCountryCode

but it throwing the below error.

Msg 102, Level 15, State 1, Line 85 Incorrect syntax near ';'.

I am unable to find whats wrong with the query.

Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77
unnikrishnan
  • 141
  • 2
  • 3
  • 15
  • Did you try it yourself in `SSMS`? If you click on the error message, `SSMS` will easily show you the invalid character in your 3rd line from end! – Md. Suman Kabir Nov 08 '17 at 05:37

2 Answers2

2

You have the encoded > instead of >:

SELECT 
ABL.Address1 AS BillingAddress1,
ABL.Address2 AS BillingAddress2,
ABL.Address3 AS BillingAddress3,
ABL.Address4 AS BillingAddress4,
ABL.UnitNumber AS BillingUnitNumber,
ABL.City AS BillingCity,
ABL.ProvinceOrState AS BillingProvinceOrState,
ABL.PostalCode AS BillingPostalCode,
CASE ISNULL (ABL.CountryCode,'')
                                WHEN 'CAN' THEN 'CANADA'
                                WHEN 'CA' THEN 'CANADA'
                                WHEN 'US' THEN 'USA'    
                                WHEN '' THEN
CASE WHEN CHARINDEX(RTRIM(ABL.ProvinceOrState),'BC,NWT,SK,MB,ON,QC,NB,NS,PEI,NF') > 0  
THEN 'CANADA'
ELSE NULL
END
ELSE ABL.CountryCode                
END AS BillingCountryCode
CristiC
  • 22,068
  • 12
  • 57
  • 89
1

I see there is a text &gt ; in the case statement instead of >

SELECT
ABL.Address1 AS BillingAddress1,
ABL.Address2 AS BillingAddress2,
ABL.Address3 AS BillingAddress3,
ABL.Address4 AS BillingAddress4,
ABL.UnitNumber AS BillingUnitNumber,
ABL.City AS BillingCity,
ABL.ProvinceOrState AS BillingProvinceOrState,
ABL.PostalCode AS BillingPostalCode,
CASE ISNULL (ABL.CountryCode,'')
                                WHEN 'CAN' THEN 'CANADA'
                                WHEN 'CA' THEN 'CANADA'
                                WHEN 'US' THEN 'USA'    
                                WHEN '' THEN
CASE WHEN CHARINDEX(RTRIM(ABL.ProvinceOrState),'BC,NWT,SK,MB,ON,QC,NB,NS,PEI,NF') > 0                                                                                THEN 'CANADA'                                                                    ELSE NULL                                                                              END
ELSE ABL.CountryCode                
END AS BillingCountryCode

Try This one - I just replaced the string "&gt ;" with > symbol

This might have happened when you copied the code form any other window or text editor

Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39