Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
midgard
Starting Member
2 Posts |
Posted - 2012-07-10 : 02:10:25
|
Hi Folks.I would like to trade this code for a simple use case ...SELECT TOP 5 ID, CASE WHEN ISNUMERIC(FUT_UMLAGERUNG.LZL_STAT_EKDM) = 0 THEN 'IS NOT NUMERIC' -- CAST(FUT_UMLAGERUNG.LZL_MENGE AS numeric(15,2)) ELSE 'IS NUMERIC' -- CAST(FUT_UMLAGERUNG.LZL_MENGE AS numeric(15,2)) END AS SUMME_EK, ISNUMERIC(FUT_UMLAGERUNG.LZL_MENGE) AS LZL_MENGE_NUMERIC, CAST(FUT_UMLAGERUNG.LZL_MENGE AS numeric(15,2)) AS LZL_MENGE_VALUE, ISNUMERIC(FUT_UMLAGERUNG.LZL_STAT_EKDM) AS LZL_MENGE_NUMERICFROM FUT_UMLAGERUNGResult:ID SUMME_EK LZL_MENGE_NUMERIC LZL_MENGE_VALUE LZL_MENGE_NUMERIC1 IS NOT NUMERIC 1 1.00 02 IS NUMERIC 1 2.00 13 IS NUMERIC 1 2.00 14 IS NUMERIC 1 2.00 15 IS NUMERIC 1 120.00 1So far, so good. But if I cast the field 'FUT_UMLAGERUNG.LZL_MENGE' in the case, I receive the error:Msg 8114, Level 16, State 5, Line 2Error converting data type varchar to numeric.Any idea?Thanxxx Patrick |
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-07-10 : 02:21:27
|
It's most likely this bit outside of the case statement:CAST(FUT_UMLAGERUNG.LZL_MENGE AS numeric(15,2)) you can't just cast it - what if it's not numeric?You'll need to conditionally cast it in a case statement as you've done with SUMME_EK and decide what you want to do with non-numerics (null or zero maybe?)Also, your TOP 5 without order by does not make any sense. You need to order by (and carefully consider if you really mean top 5 rows or rows that have the top 5 value). |
 |
|
midgard
Starting Member
2 Posts |
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-07-10 : 02:39:20
|
Good for you, but your query still has holes in it.1) You are casting values without testing for IS_NUMERIC outside the cast so it can still fail.2) without ORDER BY your TOP makes no sense. |
 |
|
|
|
|
|
|