Search code examples
sqlsybasevelocitysqlanywhere

Cannot cast result from Sub-Select to numeric


I am selecting a list of IDs as a sub-query in a condition but it says it cannot convert '123,456' to numeric. The problem occurs in the last line. DB is Sybase-SQL-Anywhere.

SELECT
 ISNULL(SUM(a.menge), 0) AS menge,
 ISNULL(SUM(a.wert), 0) AS wert
FROM admin.p_ws_ix_kontrakte_ernte_auswertung_jensek a
WHERE
 (a.KtrErnteJahr = ? OR ? IS NULL)
AND (
 (a.KtrDispoKennz >= ? OR ? IS NULL)
AND
 (a.KtrDispoKennz <= ? OR ? IS NULL)
)
AND a.artikelstammid IN ((SELECT LIST(artikelstammId) FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column))

Solution

  • Remove the LIST():

    # replace this:
    
    AND a.artikelstammid IN ((SELECT LIST(artikelstammId) FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column))
    
    # with this:
    
    AND a.artikelstammid IN (SELECT artikelstammId FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column)
    

    Another option would be an exists/correlated subquery:

    # replace this:
    
    AND a.artikelstammid IN ((SELECT LIST(artikelstammId) FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column))
    
    # with this:
    
    AND exists (SELECT 1 FROM admin.ws_ix_auswertung_cfg_spalten_artikel b WHERE b.columnId = $column and b.artikelstammId = a.artikelstammid)