Search code examples
sqlgoogle-bigquerypublic

Anyone has any problems with "IF" function in Big Query?


I've been writing a query in big query cause I'm still learning, so I use the sandbox to write my sentences, but I don't get success, I try so many times in so many ways but I don't. Is there some special condition to the 'IF' function that works?

SELECT stn,date,

FROM `bigquery-public-data.noaa_gsod.gsod2020`

IF(temp=9999,9, NULL, temp) AS temperature,

I tring to solve it a one week later, and already try do mane times in so many ways to arrive in one result that not return erro in the Big Query, I hope that someone analysing and gimme one answer to it! Thank you!


Solution

  • The IF function should be used in the SELECT part, example :

    WITH Numbers AS (
      SELECT 10 as A, 20 as B UNION ALL
      SELECT 50, 30 UNION ALL
      SELECT 60, 60
    )
    SELECT
      A,
      B,
      IF(A < B, 'true', 'false') AS result
    FROM Numbers
    
    +------------------+
    | A  | B  | result |
    +------------------+
    | 10 | 20 | true   |
    | 50 | 30 | false  |
    | 60 | 60 | false  |
    +------------------+
    

    In your code snippet the IF is placed the FROM without WHERE condition, it can't work like that.