Re: Weird Cast operator

From: Erland Sommarskog (esquel_at_sommarskog.se)
Date: 09/17/04


Date: Fri, 17 Sep 2004 22:10:10 +0000 (UTC)

Ganesan Rajaraman (ganesanrajaraman@gmail.com) writes:
> We know that this is not true. Please refer to @outFails sql from my
> first post. If it were to evaluate THEN clause only when WHEN is true,
> why @outFails fails?

Let's go back to your queries:

    select @outWorks = CASE
                            WHEN RTrim(LTrim(@in)) NOT LIKE '%[^0-9]%'
                                THEN Cast(Cast(@in as int) as varchar)
                            ELSE @in
                          END

The datatype of this CASE expresion is varchar. Both branches return
varchar, so that is not strange.

   select @outFails = CASE
                            WHEN RTrim(LTrim(@in)) NOT LIKE '%[^0-9]%'
                                THEN Cast(@in as int)
                            ELSE @in
                          END

Here, the value of the CASE expression is int. This is because the two
bracnhes returns different datatype. Therefore there will be an automatic
conversion to the data-type that has the highest priority of the two
in the SQL datatype precedence list. Varchar has lower precendence than
int, so thus the value is int.

If @in = '003' the value matches the WHEN condition, and thus expression
returns 3. But if @in is '00-3', you end up in the ELSE branch. Problem
though is that '00-3' cannot be converted to int, so the statement
fails.

-- 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp


Relevant Pages

  • Re: Weird Cast operator
    ... I do understand that converting '00-3' to int will not be ... >>(like in the first sql in my posting), server has to do CAST(@in AS ... Please refer to @outFails sql from my ... If it were to evaluate THEN clause only when WHEN is true, ...
    (microsoft.public.sqlserver.programming)
  • update question
    ... I am new to working with SQL and I have a few questions. ... update query below, one will it work and I looking for a better ... street address varchar ...
    (comp.databases.sybase)
  • Re: Weird Cast operator
    ... (like in the first sql in my posting), server has to do CAST(@in AS ... INT) in order evaulation the whole expression, ... > CASE can only return a single datatype. ... > is INT and the other VARCHAR. ...
    (microsoft.public.sqlserver.programming)
  • Re: Views vs Stored Procedures, whats the difference?
    ... I'm just trying to differentiate between two fundamentally different SQL objects. ... CREATE TABLE P(pk INT NOT NULL PRIMARY KEY, ... SELECT MAXFROM nestedview ...
    (comp.databases.ms-sqlserver)
  • Re: Db2dclgn Indicator variables
    ... It is true that "49-levels" are used by most SQL products for holding ... I'd love to see a COBOL that supports the ANY LENGTH PREFIXED ... clause for a much more natural VARCHAR. ... I'd also love to see some kind of COBOL support for "null" values. ...
    (comp.lang.cobol)