Re: Weird Cast operator

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 09/16/04


Date: Thu, 16 Sep 2004 23:25:24 +0200

On 16 Sep 2004 13:22:06 -0700, Ganesan Rajaraman wrote:

>1. Try making @in string contain '003' (instead of '00-3'. You will
>notice that both sqls work. From this, we know that trying to return
>int type is not the problem.

Hi Ganesan,

That's not correct. The implicit conversion of '003' to integer will
succed and result in the value 3; the implicit conversion of '0-03' to
integer will fail and result in an error message.

>2. Now, I guess my question is, even when we have
> CAST(CAST(@in AS INT) AS VARCHAR(6))
>(like in the first sql in my posting), server has to do CAST(@in AS
>INT) in order evaulation the whole expression, right. Why doesn't it
>give a syntax error while trying to do that?

Because this CAST is in the THEN clause of the CASE; it will only be
evaluated if WHEN RTrim(LTrim(@in)) NOT LIKE '%[^0-9]%' is true. For the
string value '003', it is (and conversion works flawlessly); for the
string value '0-03', the WHEN part is false and the ELSE part will be
evaluated instead.

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • RE: String "+" operator conversion
    ... Actually that is expected what you are doing is an implicit conversion. ... int a = 10 ... we force the compiler to first convert the long to a string, then parse that string to turn it into an integer) now we can add them all together. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why?
    ... obvious that an attempt to assign an 'int' type to a 'String' type is ... syntactically incorrect since there is no implicit conversion of 'int' type ...
    (comp.lang.java.help)
  • Re: Pass value of short type
    ... unsigned short into an unsigned int, ... be no warning, because the data will always fit. ... the implicit conversion is a promotion from unsigned short ... I agree you should avoid casts whenever possible (and especially avoid ...
    (comp.lang.cpp)
  • Re: Primitive types and implicit conversions
    ... > as just using an int, ... > If i use the suffix of 'U', am i then working around the implicit conversion ... > So, is it so, that when assigning a value to ushort, an implicit conversion ... instantiating a ushort instance(which should be more efficient than ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Runtime errors possible when I forgot include
    ... ie integer promotion and float->double. ... If implicit conversion between ... int and char* is unsafe < sizeof ...
    (comp.lang.c)