postgresql 将文本转换为 Varchar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38917960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:17:59  来源:igfitidea点击:

Convert Text To Varchar

postgresql

提问by PinkSmelly BlueSocks

In Postgresqlhow do you convert a text field to a varchar? I have tried both of the below, but neither convert my text field to varchar.

Postgresql你怎么一个文本字段转换为varchar?我已经尝试了以下两种方法,但都没有将我的文本字段转换为 varchar。

Cast(Iamtextfield As Varchar)
Char(Iamtextfield)

回答by Chris Travers

text is just another term for unlimited varchar in PostgreSQL.

text 只是 PostgreSQL 中无限制 varchar 的另一个术语。

But if you want to make sure the type is set correctly in the return output, simply:

但是,如果您想确保在返回输出中正确设置了类型,只需:

 iamtextfield::varchar

Or if it is case sensitive

或者如果它区分大小写

 "Iamtextfield"::varchar

If you want to truncate you can do something like:

如果要截断,可以执行以下操作:

 iamtextfield::varchar(5)

回答by Piotr R

Cast in postgress, text to varchar example:

在 postgress 中转换,文本到 varchar 示例:

select (select 'example text'::text)::varchar