Using a not equal in SQL Case Expression

Question

Monday, July 22, 2013 8:56 PM

The SUBSTRING function returns @domain.com.  I don't want any results to be displayed from my sql statement if it equals @myemail.com. I'm getting an error that says invalid syntax near <

How can I fix this error? Thanks

SELECT DISTINCT CASE CUST_EMLAD_TX      WHEN SUBSTRING(CUST_EMLAD_TX, CHARINDEX('@',CUST_EMLAD_TX), LEN(CUST_EMLAD_TX) <> '@myemail.com') THEN CUST_EMLAD_TX    ENDFROM ...

All replies (5)

Tuesday, July 23, 2013 1:03 AM ✅Answered

You need the "Searched" CASE construct in this case. In addition, one of the closing parenthesis was incorrectly placed.

Try:

SELECT DISTINCT   CASE     WHEN SUBSTRING(CUST_EMLAD_TX,         CHARINDEX('@',CUST_EMLAD_TX),         LEN(CUST_EMLAD_TX))<> '@myemail.com'       THEN CUST_EMLAD_TX  ENDFROM ...

Van Dinh


Monday, July 22, 2013 8:57 PM

The SUBSTRING function returns @domain.com.  I don't want any results to be displayed from my sql statement if it equals @myemail.com. I have other cases too that are not relevant to my question. I'm getting an error that says invalid syntax near <

How can I fix this error? Thanks

SELECT DISTINCT CASE CUST_EMLAD_TX      WHEN SUBSTRING(CUST_EMLAD_TX, CHARINDEX('@',CUST_EMLAD_TX), LEN(CUST_EMLAD_TX) <> '@myemail.com') THEN CUST_EMLAD_TX    ENDFROM ...

Monday, July 22, 2013 9:20 PM

Try moving one of those parens:

SELECT DISTINCT CASE CUST_EMLAD_TX
      WHEN SUBSTRING(CUST_EMLAD_TX, CHARINDEX('@',CUST_EMLAD_TX), LEN(CUST_EMLAD_TX)) <> '@myemail.com' THEN CUST_EMLAD_TX
    END
FROM .

Tuesday, July 23, 2013 4:57 AM

When you specify the conditional statement after WHEN , then nothing should be mentioned after CASE.

It can be like this,

select CASE WHEN 1=1 then 'Equal' else 'not equal' end
--or
select CASE 1 WHEN 1 then 'Equal' else 'not equal' end
--this throws error 
select CASE 1 WHEN 1=1 then 'Equal' else 'not equal' end

Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.


Tuesday, July 23, 2013 12:48 PM

This is exactly what I needed. Thanks a lot !