0

Query

SELECT TOP 1000
CASE WHEN VRI.Square_Footage <> '' 
THEN VRI.Square_Footage 
ELSE 
    CASE WHEN VRI.Property_Type = 'LAND' 
        THEN CAST((CONVERT(NUMERIC(38, 3),VRI.Acres)*43560) AS DECIMAL) 
    ELSE 
        VRI.Lot_Size 
    END 
END
FROM View_Report_Information_Tables AS VRI

Even if I checked for VRI.Acres with isnumeric(), it still yield the same exact error? How can I fix this problem?

1
  • @Conrad Frix: Thanks for the edit! Btw, you have a very cool avatar ;)! Commented Apr 25, 2011 at 16:34

2 Answers 2

2

ISNUMERIC doesn't guarantee that it will successfully cast to decimal.

SELECT ISNUMERIC('£100') /*Returns 1*/

SELECT CONVERT(NUMERIC(38, 3),'£100') /*Error*/

Additionally all branches of the case statement need to return compatible datatypes. It looks like VRI.Square_Footage is a string.

Does this work for you?

SELECT TOP 1000 CASE
                  WHEN ISNUMERIC(VRI.Square_Footage + 'e0') = 1 THEN
                  VRI.Square_Footage
                  WHEN VRI.Property_Type = 'LAND'
                       AND ISNUMERIC(VRI.Acres + 'e0') = 1 THEN CAST((
                  CONVERT(NUMERIC(38, 3), VRI.Acres) * 43560 ) AS DECIMAL)
                  WHEN ISNUMERIC(VRI.Lot_Size + 'e0') = 1 THEN VRI.Lot_Size
                END
FROM   View_Report_Information_Tables AS VRI  
Sign up to request clarification or add additional context in comments.

9 Comments

@Martin: Thanks. Yes, VRI.Square_Footage is actually varchar. How can I work around this issue?
This. You can't mix strings and numbers in the same column. One of them must be converted to the other.
@Jerry Ritcey: So that means I have to convert all varchar to decimal?
@Chan - The only way of mixing (non numeric) strings and numbers would be a cast to sql_variant or cast the numbers as strings. Can you provide an example of the data that fails?
@Conrad Frix: I would agree with you. Unfortunately, this belongs to SQL team, I'm not in charge of it. Btw, I just converted it back to varchar and the sql query run fine.
|
2

Here run this

 SELECT ISNUMERIC('d25')

That can be converted to float but not decimal/numeric

SELECT CONVERT(FLOAT,'2d5')

As you can see, you can't depend on numeric for decimal/numeric data types

Take a look at IsNumeric, IsInt, IsNumber for some code that will show you how you can check

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.