比较varchar和char [英] Compare varchar with char

查看:76
本文介绍了比较varchar和char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在比较 varchar char 时,我试图弄清楚Postgres的工作。

I am trying to figure out what Postgres does when comparing varchar with char.

这是我的测试之一:

test=# select 'a'::character varying = 'a     '::character;
?column?
----------
 t

test=# select 'ab'::character varying = 'ab     '::character;
?column?
----------
 f

我的虫子。有人知道这是怎么回事吗?

This looks like a bug to me. Does anyone know what is going on here? Are there good documents on this topic?

推荐答案

根本不是错误。

强制转换字符串文字'ab'转换为字符,这是您得到的:

Not a bug at all.
Casting the string literal 'ab ' to character, this is what you get:

a

每个文档:

$ b没有长度说明符的$ b

字符等效于字符(1)

character without length specifier is equivalent to character(1).

'a':: character(1)然后将被强制为 varchar 个字符变化)以测试是否与'a':: varchar 'ab':: varchar 并分别产生 TRUE FALSE

'a'::character(1) will then be coerced to varchar (character varying) to test for equality with 'a'::varchar or 'ab'::varchar and yield TRUE or FALSE respectively.

基本上,几乎没有充分的理由完全使用 c 字符。这是一种旧式的,已经不再有用。只需使用文本 varchar

Basically, there is hardly any good reason to use character at all. It's a legacy type that has outlived its usefulness. Just use text or varchar.

  • Any downsides of using data type "text" for storing strings?

这篇关于比较varchar和char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆