Search code examples
sqlcase

Does case statement work in bigquery sandbox?


I'm getting this error when I tried to run the query:

No matching signature for operator CASE; all THEN/ELSE arguments must be coercible to a common type but found: BOOL, STRING; actual argument types (WHEN THEN) ELSE: (BOOL BOOL) STRING at [3:2]

**Query **

SELECT  
 customer_id,
 CASE 
  WHEN name = 'Devadas Sloan' THEN name = 'Devdas Sloan'
  ELSE name
  END AS new_name
FROM `valid-cell-380712.customer_table_gda.customer_table` 

Solution

  • You are trying to do an assignment inside the CASE statement, when all you need is to generate an if and else value. Use this version:

    SELECT
        customer_id,
        CASE WHEN name = 'Devadas Sloan' THEN name ELSE name END AS new_name
    FROM valid-cell-380712.customer_table_gda.customer_table;
    

    Of course, the above CASE expression just returns name in all cases. It would make more sense if you wanted to replace Devadas Sloan with some other name, e.g. use:

    SELECT
        customer_id,
        CASE WHEN name = 'Devadas Sloan' THEN 'D Sloan' ELSE name END AS new_name
    FROM valid-cell-380712.customer_table_gda.customer_table;