Incorrect syntax near '='.

Lylyy 380 Reputation points
2023-11-15T02:36:09.44+00:00

Trying to write a case condition query, but fail to this error: Incorrect syntax near '='.

Here is my query:

select case field1         
       when field1 = '1' then 'true'         
       when field1 = '0' then 'false' end 
FROM temptable

Tried removing single quotes of 1, but not worked.

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 31,566 Reputation points
    2023-11-15T02:51:12.84+00:00

    Hi @Lylyy

    It is a syntax error, see this doc: CASE (Transact-SQL)

    Try this:

    select case 
           when field1 = '1' then 'true'              
           when field1 = '0' then 'false' end 
    FROM temptable  
    
    --or  
    
    select case field1        
           when '1' then 'true'         
           when '0' then 'false' end 
    FROM temptable
    

    Best regards,

    Cosmog Hong


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.