Skip to main content
Question

CASTING TO VARCHAR

  • August 30, 2019
  • 2 replies
  • 14 views

MaciejPaliwoda
Forum|alt.badge.img+2

Hello TEAM,
Could someone explain me the difference in CASTING to VARCHAR :

a=> select 'abcd'::"VARCHAR", 'abcd'::VARCHAR;
?column? | ?column?
----------+----------
a | abcd
(1 row)
is it a bug or expected behaviour, that double quoting the DATATYPE NAME change its length to** (1)** instead default 80?
Thanks
M.

2 replies

Jim_Knicely
Forum|alt.badge.img+2
  • Participating Frequently
  • August 30, 2019

That is interesting! "VARCHAR" does indeed imply VARCHAR(1)...

dbadmin=> select 1::"VARCHAR", 'abcd'::VARCHAR;
 ?column? | ?column?
----------+----------
 1        | abcd
(1 row)

dbadmin=> select 123::"VARCHAR", 'abcd'::VARCHAR;
ERROR 3589:  Integer '123' is too long for type Varchar(1)

Also, interesting that you cannot use INT or FLOATin double quotes:

dbadmin=> SELECT 123::"INT";
ERROR 5108:  Type "INT" does not exist
dbadmin=> SELECT 123::"INTEGER";
ERROR 5108:  Type "INTEGER" does not exist
dbadmin=> SELECT 123::"FLOAT";
ERROR 5108:  Type "FLOAT" does not exist

But DATE works:

dbadmin=> SELECT '09-19-2019'::"DATE";
  ?column?
------------
 2019-09-19
(1 row)

Jim_Knicely
Forum|alt.badge.img+2
  • Participating Frequently
  • August 30, 2019

One more:

dbadmin=> CREATE TABLE test (c "VARCHAR");
CREATE TABLE
dbadmin=> INSERT INTO test SELECT 'abcd';
ERROR 8682:  String of 4 octets is too long for type Varchar(1) for column c

@MaciejPaliwoda - Probably a bug and I think it should honor the DfltAttrSize configuration parameter, which as you pointed out, defaults to 80.