简体   繁体   中英

SQL Server : SUM of column with alphanumeric values

I want to get the sum of a column that is alphanumeric. I want to add numeric values and return column values if not numeric. What I did is a added a CASE WHEN that looks like this

CASE 
   WHEN intAllocatedResourceperDivision NOT IN ('CL', 'HS', 'HV', 'ML', 'SL', 'VL', 'HC', 'S', '*') 
      THEN CAST(SUM(ISNULL(CAST(intAllocatedResourceperDivision AS DECIMAL), 0.00)) AS NVARCHAR(250)) 
      ELSE intAllocatedResourceperDivision 
END intAllocatedResourceperDivision

So I assume that all numeric values will be added and if values is in ('CL', 'HS', 'HV', 'ML', 'SL', 'VL', 'HC', 'S', '*') it will be returned as is.

But I'm getting

Error converting data type nvarchar to numeric.

Looks like your SUM aggregation is out of place. You only sum if the condition in the case statement is true. Try this:

SUM(case when intAllocatedResourceperDivision NOT IN ('CL','HS','HV','ML','SL','VL','HC','S','*') THEN intAllocatedResourceperDivision else 0 end)

In case you don't know the exact potential combination of non numeric values, you can use the ISNUMERIC function (assuming you're using SQL Server) to test whether or not the value is a number and assign a 0 if it's not, aggregating the final result.

SUM(case when ISNUMERIC(intAllocatedResourceperDivision) = 1 then intAllocatedResourceperDivision else 0 end)

To aggregate the numerical values and also keep non-numerical, you can use a union query as such:

select
    cast(SUM(cast(intAllocatedResourceperDivision as decimal(18,2))) as varchar)
from
    YOUR_TABLE
where
    ISNUMERIC(intAllocatedResourceperDivision) = 1

UNION ALL

select
    intAllocatedResourceperDivision
from
    YOUR_TABLE
where
    ISNUMERIC(intAllocatedResourceperDivision) = 0

This looks like SQL Server syntax. I would recommend using TRY_CONVERT() :

TRY_CONVERT(DECIMAL, intAllocatedResourceperDivision) as intAllocatedResourceperDivision

Try this:

select 'CL' as intAllocatedResourceperDivision into #tmp
union select 'HS'union select 'HV'union select 'ML'union select 'SL'union select 'VL'union select 'HC'union select 'S'union select '*'union select '1'union select '4'

select CAST(SUM(ISNULL(CAST(intAllocatedResourceperDivision AS DECIMAL),0.00)) AS nvarchar(250)) as intAllocatedResourceperDivision
from #tmp where intAllocatedResourceperDivision NOT IN ('CL','HS','HV','ML','SL','VL','HC','S','*')
union 
select intAllocatedResourceperDivision
from #tmp where intAllocatedResourceperDivision IN ('CL','HS','HV','ML','SL','VL','HC','S','*')

cast your as varchar to match your IN operator values.

,CASE WHEN cast(intAllocatedResourceperDivision as varchar(2)) 
    NOT IN ('CL','HS','HV','ML','SL','VL','HC','S','*') 
 THEN CAST(SUM(ISNULL(CAST(intAllocatedResourceperDivision AS DECIMAL),0.00)) as nvarchar(250)) 
 ELSE cast(intAllocatedResourceperDivision as nvarchar(250))  END intAllocatedResourceperDivision

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM