Group By Sum with Substring SQL Syntax

I have a situation where I need to total by similar but not exact strings. Example data:

Cust ID    Val 1    Val 2    Val 3
448          20        30         40
448-01     30        40         50
448-02     60        70         80

What I want is one line totalling for 448 to be 110,140 and 170
The dash may not be in the third postion so I want to locate its position and then total the group.
My code below does not work because there are customer ID's that have no dash and the query bombs. How can I adjust the structure to produce the desired results.
select sum(agperamt_1),sum(agperamt_2),sum(agperamt_3),sum(agperamt_4),sum(onordamt)
from rm00103
where custnmbr like '448%' 
group by left(custnmbr,patindex('%-%',custnmbr)-1)

Open in new window

LVL 1
rwheeler23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
select sum(agperamt_1),sum(agperamt_2),sum(agperamt_3),sum(agperamt_4),sum(onordamt)
from rm00103
where custnmbr like '448%'
group by case when charindex('-', custnmbr) = 0 then custnmbr else left(custnmbr,patindex('%-%',custnmbr)-1) end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Reza RadConsultant, TrainerCommented:
try this one

select sum(agperamt_1),sum(agperamt_2),sum(agperamt_3),sum(agperamt_4),sum(onordamt)
from rm00103
group by left(custnmbr,case patindex('%-%','448') when 0 then LEN('448') else patindex('%-%','448')-1 end)

Open in new window

0
Reza RadConsultant, TrainerCommented:
select sum(agperamt_1),sum(agperamt_2),sum(agperamt_3),sum(agperamt_4),sum(onordamt)
from rm00103
where custnmbr like '448%'
group by left(custnmbr,case patindex('%-%','448') when 0 then LEN('448') else patindex('%-%','448')-1 end)
0
Reza RadConsultant, TrainerCommented:
sorry, this one is correct:

select sum(agperamt_1),sum(agperamt_2),sum(agperamt_3),sum(agperamt_4),sum(onordamt)
from rm00103
where custnmbr like '448%' 
group by left(custnmbr,case patindex('%-%',custnmbr) when 0 then LEN(custnmbr) else patindex('%-%',custnmbr)-1 end)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.