如何将 varchar 转换为布尔值 [英] How to cast varchar to boolean
本文介绍了如何将 varchar 转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量x",它在临时表中是 varchar,但它在目标表中设置为布尔值,它具有真"和假"值.如何在 postgresql 中将 varchar 转换为 boolean?
I have a variable 'x' which is varchar in staging table, but it is set to boolean in target table which has 'true' and 'false' values. How can I convert varchar to boolean in postgresql?
推荐答案
如果 varchar 列包含 其中一个字符串(不区分大小写):
If the varchar column contains one of the strings (case-insensitive):
t
、true
、y
、yes
、on
、1
f
、false
、n
、no
、off
、0
t
,true
,y
,yes
,on
,1
f
,false
,n
,no
,off
,0
您可以简单地将其转换为布尔值,例如:
you can simply cast it to boolean, e.g:
select 'true'::boolean, 'false'::boolean;
bool | bool
------+------
t | f
(1 row)
请参阅 SQLFiddle.
这篇关于如何将 varchar 转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文