views:

18

answers:

3

Hi, I'm trying to trim some info from a table. The column has a number and then a word (e.g. 5 Apples). I just need the number so that I can sum the total of apples. I can't use count because I need to go by the value (e.g. 5 Apples for one, 3 Apples for the other) and count will just return that there are 2 entries and not pull the 5 and 3.

I've tried using SUM but it doesn't work either.

Can anyone help (Or point me to a tutorial) that will explain how I can trim/extract info from the columns value? I've been looking and have only been able to find how to trim spaces (Which would be nice if I could trim everything after spaces too, then I'd just be able to trim away after the number)

Any help is greatly appreciated! Thanks

A: 

Sounds like you need to SubString with CharIndex, Cast/Convert the resulting 'number' to an int and then use SUM.

Assuming your 'number' is always followed by 'Apples' (untested):

SELECT SUM(CONVERT(int,SUBSTRING(column_name,0,CharIndex(' Apple',column_name)-1))) as numApples FROM table_name;
Mark
Thanks! But I can't vote you because stackoverflow refuses to recognize my log in even though I went through the open id system
CSharpProgrammer
Thanks for the revision, I wish I was able to declare too proper answers
CSharpProgrammer
A: 

A combination of sum, cast, substring, charindex will do the work i think:

Find the first occurence of whitespace, take the substring of the column until the first whitespace position, cast it as an integer and sum it up.

SELECT SUM(CAST(SUBSTRING(yourColumn, 1, CHARINDEX(' ', yourColumn) -1) AS INT)) 
FROM yourTable

Edit: first char is 1 in substring

JanW
Thanks, I was playing with it a bit to get it to work before the edit It works fine. Thanks a lot, I'd up vote you but as I said on the last answer, stackoverflow doesn't trust my open id to let me log in for some reason.
CSharpProgrammer
+1  A: 

You can use patindex to search for the first non-digit, and then substring to get only the numeric part of the string:

declare @fruit table (id int identity, description varchar(50))
insert @fruit (description) select '3 apples'
union all select '10 apples'
union all select '12 apples'

select  sum(cast(substring(description, 1, FirstNonDigit) as int))
from    (
        select  patindex('%[^0-9]%', description) FirstNonDigit
        ,       description
        from    @fruit
        ) as SubQuery
where   FirstNonDigit > 0

This prints 25.

Having said that, a good table design would put the number and description in two different columns.

Andomar
Agree to your last statement. A two column design would make much more sense. If the number of products (e.g. Apple, Lemons, ...) is fixed, i would also suggest to make a products table and link a FK to it in the above table.
JanW
Well my example isn't exactly carbon copy, but for the sake of the example, it'd only ever be apples. Never lemons or anything else. For the purpose of the column and this table, it will always be an apple. If it were ever to be apples and lemons, it'd definitely be wise to change it to a number and then append the proper fruit (apple or lemons or w/e) onto it, but for what it will always be now, don't fix what's not broke, right?
CSharpProgrammer
Okay if you are only handle apples, a type table will make no sense agree ;) But you should consider Andromar's suggestion, depending on how many data is queried and how often the data is queried. If its a lot, you should indeed use two columns, because the above accepted answer is very inefficient, regarding a two column version. Greets jan
JanW
Well the type was just an example for the sake of simplicity. The column in this table is only going to hold the same things in this scenario.I understand the benefit of splitting it, I don't wish to imply I'm scoffing at the idea of it, it's just for the way it's set up currently, it works. I imagine if it ever becomes a problem, it'd be well worth the time to look at splitting it up to make it more efficient :)
CSharpProgrammer