Archives of the TeradataForum
Message Posted: Wed, 27 Mar 2002 @ 15:25:46 GMT
Subj: | | Re: A question on Casting |
|
From: | | Geoffrey Rommel |
| I have a very simple question on casting, should the following return the same result or no, if yes - why? And if not - why? | |
select 1(char); /* Implicit Cast */
select cast(1 as char); /* Explicit Cast */
Should they? Well, probably they should, but they don't.
(Side note: Actually, both of these are explicit casts. The following would result in an implicit cast:
insert into hey_jude ( char_column )
select numeric_column from nowhere_man;)
The difference is that the first is a Teradata cast, the second an ANSI cast. In your Teradata cast, you are getting only the
first character of the result, which happens to be a blank. Consider the following results.
select 1(char(20)); /* Teradata version */
1
--------------------
1
select cast(1 as char(20)); /* ANSI */
1
--------------------
1
I don't know why ANSI is defined the way it is, but I think I can explain the Teradata version. The literal 1 is implicitly
defined as the smallest data type it will fit in, namely BYTEINT. Since BYTEINTs can range from -128 to +127, the default format for them is
'---9', and so casting them to character will always require at least 4 bytes.
|