如何将 varchar 转换为 bool postgresql

how to cast varchar to bool postgresql

我需要从一个 table 中读取值作为 varchar 并将其作为布尔值写入另一个。这样除了 "False" 之外的任何标签(文本)都被视为 "True"。但我也想要转换(转换)工作 - ::bool。 我有例子:

case when COALESCE(NULLIF('#(OG)', '')::bool, false) = FALSE then FALSE else TRUE end

但它会出错,因为在我的 table 中我没有完美的数据,例如:f, false, n, no, off, 0 and t, true, y, yes, on, 1,还有简单的文本 - 123.

invalid input syntax for type boolean: "123"

我使用 EXEL 文件,我所做的一切都是在 file.manifest:

<?xml version="1.0" encoding="utf-8"?>
<Manifest>
<File startRow="2" table="td_actualpayments">
    <add isSql="false" key="OG" cell="G" header="Contractor OG" />
    <add isSql="true" key="ContractorIsGroupSociety" dataType="subquery" query="(case when COALESCE(NULLIF('#(OG)', '')::bool, false) = FALSE then FALSE else TRUE end)" />
  </File>
<PostUpdate>

您可以使用正则表达式,例如

NOT COALESCE(textcol, '') ~* E'^((f(a(l(se?)?)?)?)|(off?)|(no?)|0?)$'

对于 NULL、空字符串和 FALSE 的每个有效字符串表示应该 return FALSE,对于所有 return TRUE其他字符串。