使用postgresql中的case语句将列类型自动转换为布尔值



我需要将所有"1"替换为yes,将"0"替换为no。我编写的查询是:

second_tranche_inspection_reporting_questionnaire.is_there_a_sill_band = case
when second_tranche_inspection_reporting_questionnaire.is_there_a_sill_band = '1' then 'YES'
when second_tranche_inspection_reporting_questionnaire.is_there_a_sill_band = '0' then 'NO'
end as is_there_a_sill_band,

其中"second_tranch_inspection_reporting_questionary"是我的表名,"is_there_a_sill_band"是我类型为"text"的列名。没有任何错误。但问题是使用它将列类型转换为布尔型,无论它是"0"还是"1",我得到的结果都是"f"。请帮忙。我正在使用postgresql9.4和pg-admin III.

您看到的是is_there_still_a_band的当前值和新值之间的比较结果,该结果将始终为false。

选择应该如下所示:

SELECT
CASE
WHEN is_there_a_sill_band = '1' THEN 'YES'
WHEN is_there_a_sill_band = '0' THEN 'NO'
END AS is_there_a_sill_band
FROM second_tranche_inspection_reporting_questionnaire;

您的选择相当于上面的选择,但有一个无关的比较,它总是评估为false:

SELECT
is_there_a_sill_band =
CASE
WHEN is_there_a_sill_band = '1' THEN 'YES'
WHEN is_there_a_sill_band = '0' THEN 'NO'
END AS is_there_a_sill_band
FROM second_tranche_inspection_reporting_questionnaire;

例如,如果当前值为1,则case语句的求值结果为YES。您正在选择'1' = 'YES' AS is_there_a_sill_band'1' = 'YES'是一个布尔语句,其求值结果为false

最新更新