ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

CHAR returns VARCHAR

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CHAR returns VARCHAR

    Given this extract of code from a CREATE VIEW


    Code:
    ...as
    select char('SAP107', 10),
           char(CBGRP, 16),
           CBCUST,CBYEAR,CBPERC,CBPERN,CBOI,CBTID,CBREG,CBSLSM,
           char('REV',3),
           dec(CBREV,13,2)
      from SAP107
    union
    select 'SAP107',CBGRP,...

    The first column, char('SAP107', 10), is returning a VARCHAR.
    I thought CHAR returned a fixed length character.
    CAST does the same thing.

    Is there something I'm missing?
    How do I get that first column to be CHAR(10) and not VARCHAR(10)?
    (Same with char('REV',3), it's returning VARCHAR too).

  • #2
    char() returns char(10) in your first field but the resulting type of your entire query is varchar(10) because of your union, in cases like this it determines the data type from some rules of precedence - didn't try to find them. The second part or your union uses a character string constant, 'SAP107' that is varchar because of this line in the manual "A character-string constant specifies a varying-length character string.". You would need the char().... added to the second part of your union too, then you will get the result you want. Or put your entire existing query in a CTE and then just use the char() function on a final select from that CTE so you only have it in one place.

    Comment

    Working...
    X