views:

36

answers:

3

I have to execute a statement like ( i need and keyword along with when ).

  select 
          'Is Allowed'= case A.Column
                  when 
                     A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
                  else
                    'No' end from TableName

I am getting syntax error,how to rewrite it without affecting the condition.

A: 
SELECT
    CASE A.Column
        WHEN 'Is Allowed THEN 'First'
        WHEN 2 THEN 'Second'
        WHEN 3 THEN 'Third'
        ELSE 'Other'
    END

Is the general way of making a CASE (which is your question). However, your query/logic looks a bit convoluted. More detailed answer / query is possible, but would perhaps use more statements/ nested CASE.

Tobiasopdenbrouw
+6  A: 

Try:

select case when A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
       else 'No' end as 'Is Allowed' 
from TableName
Jonathan
Good grokking, I think. +1
Tobiasopdenbrouw
A: 

Have a closer look at CASE (Transact-SQL)

SELECT  CASE
            WHEN some boolean expression
                THEN value
            ELSE default value
        END

or

SELECT  CASE value to check
            WHEN vlue to check agains
                THEN value
            ELSE default value
        END
astander