SQL Sum Help please

Get help on programming - C++, Java, Delphi, etc.
Post Reply
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

SQL Sum Help please

Post by SBSP »

I dont understand the following

@FIELD23NUM = isnull(sum(TOTALCOST),0)


@FIELD23NUM is a declared value and so is TOTALCOST

isnull dunno what the hell that is.

But what does TOTALCOST = to ?

if no where in my SQL script Totalcost is set to be value ?

Its declared as @TOTALCOST as money and thats it ?
maxxis
Moderator Emeritus
Posts: 8307
Joined: 30 Jun 2004, 02:00
Location: ( . Y . )
Contact:

Post by maxxis »

Wont TOTALCOST be the result of the sum?

My logic is select sum(*) as TOTALCOST ?
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

maxxis wrote:Wont TOTALCOST be the result of the sum?

My logic is select sum(*) as TOTALCOST ?
Im using find and only find Total cost 3 times

When its declared and the above and at the end where its set to be 0
its weird.

One more question what do i use to mid a string in a Stored proc Eg.

20000 but i only want the first to chars Mystring = mid('20000',1,2) ?
FTB_Screamer
Registered User
Posts: 424
Joined: 28 Jun 2006, 02:00
Location: Johannesburg
Contact:

Post by FTB_Screamer »

I think, isNull returns true if the sum of TOTALCOST columb in whatever table you are trying to query, is NULL(Does not return a value) else it will return 0.

if there isn't a columb in the table named TOTALCOST, then the query might be wrong and should be "@FIELD23NUM = isnull(sum(@TOTALCOST),0)" if you are trying to check if the @TOTALCOST parameter is null or not.
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

maxxis wrote:Wont TOTALCOST be the result of the sum?

My logic is select sum(*) as TOTALCOST ?
Im using find and only find Total cost 3 times

When its declared and the above and at the end where its set to be 0
its weird.

One more question what do i use to mid a string in a Stored proc Eg.

20000 but i only want the first to chars Mystring = mid('20000',1,2) ?
FTB_Screamer
Registered User
Posts: 424
Joined: 28 Jun 2006, 02:00
Location: Johannesburg
Contact:

Post by FTB_Screamer »

SBSP wrote: One more question what do i use to mid a string in a Stored proc Eg.

20000 but i only want the first to chars Mystring = mid('20000',1,2) ?

You can use SUBSTRING ( expression , start , length )

eg. @MyString = SUBSTRING('20000', 1, 2)
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

FTB_Screamer wrote:
SBSP wrote: One more question what do i use to mid a string in a Stored proc Eg.

20000 but i only want the first to chars Mystring = mid('20000',1,2) ?

You can use SUBSTRING ( expression , start , length )

eg. @MyString = SUBSTRING('20000', 1, 2)
Oh yes now i remember Thanks.

Ok one more question :lol:

How do i say IF VALUE(money type) < 1 and > -1 then ?
viceroy
Registered User
Posts: 3565
Joined: 27 Mar 2006, 02:00
Location: I forget

Post by viceroy »

FTB_Screamer wrote:
SBSP wrote: One more question what do i use to mid a string in a Stored proc Eg.

20000 but i only want the first to chars Mystring = mid('20000',1,2) ?

You can use SUBSTRING ( expression , start , length )

eg. @MyString = SUBSTRING('20000', 1, 2)
LEFT('20000',2)

Just so much simpler
SBSP wrote:I dont understand the following

@FIELD23NUM = isnull(sum(TOTALCOST),0)


@FIELD23NUM is a declared value and so is TOTALCOST

isnull dunno what the hell that is.

But what does TOTALCOST = to ?

if no where in my SQL script Totalcost is set to be value ?

Its declared as @TOTALCOST as money and thats it ?
Another thing...why is the ISNULL outside the sum? Bit pointless having it there and opens you up to errors in the calculations...unless you actually need it there for a specific reason

I'd have the code as

@FIELD23NUM = SUM(ISNULL(TOTALCOST,0))
SBSP wrote:How do i say IF VALUE(money type) < 1 and > -1 then ?
IF ((VALUE(money type) > -1) AND (VALUE(money type) < 1)
BEGIN
<do your stuff here>
END

You could use BETWEEN if you wanted the -1 and 1 to be included in the equation.

Would be the same as

IF ((VALUE(money type) >= -1) AND (VALUE(money type) <= 1)
BEGIN
<do your stuff here>
END
Image
FTB_Screamer
Registered User
Posts: 424
Joined: 28 Jun 2006, 02:00
Location: Johannesburg
Contact:

Post by FTB_Screamer »

why is the ISNULL outside the sum
Sum ignores null records so it shouldn't make a difference.
viceroy
Registered User
Posts: 3565
Joined: 27 Mar 2006, 02:00
Location: I forget

Post by viceroy »

you think? Good luck to you then

doesn't always
Last edited by viceroy on 30 Oct 2007, 15:42, edited 2 times in total.
Image
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

Thanks People.

Thanks Vice.

VALUE(money type)

Money type is just the type of declare money

as in declare MyValue as money so i take it i remove the brackets ?

Okay wait

I think i have found a Bug In SQL :evil:

This is the initial problem.

Code: Select all

--exec upFORMAT_NUMBER_2_DEC @FIELD23NUM,15,1,@FIELD23 output
					select @FIELD23 = RIGHT('00000000000000000000' + CAST(CAST(@FIELD23NUM * 100 AS INT) as varchar), 15) + CASE WHEN @FIELD23NUM > 0 THEN '+' ELSE '-' 
if @FIELD23NUM is smaller bigger than -1 and smaler than +1

lets say @FIELD23NUM = -0.01 * 100 if you convert it to text it = -1 but if you convert lets say -1000 * 100 to text it = 100000

why is that ?
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

Okay I modified the code abit but i get an error saying the table FIELD23 dont allow null's

How can i convert nulls to 0's as in zeros ? if it finds a null value

Oh i know my below code sucks bare with me i'm not good at this


Code: Select all

/*
declare @PLUSMINSIGN as varchar(1)
declare @FIELD23NUMNEW as money
declare @FIELD23NEW as varchar(20)
declare @FIELDREBUILT as varchar(20)
*/
					
					set @PLUSMINSIGN = LEFT(CAST(@FIELD23NUM AS varchar),1)
					

					IF @PLUSMINSIGN = '-' AND @FIELD23NUM > -1 
					set @FIELD23NUMNEW  = @FIELD23NUM * -1	
					set @FIELD23NEW = CAST(@FIELD23NUMNEW AS varchar)					
					select @FIELD23 =   RIGHT('00000000000000000000' +  @FIELD23NEW * 100 + @PLUSMINSIGN,15)
					GOTO SKIPREST;
 
					IF @PLUSMINSIGN = '+' AND @FIELD23NUM < 1 
					set @FIELD23NEW = CAST(@FIELD23NUMNEW AS varchar)	
					select @FIELD23 =   RIGHT('00000000000000000000' +  @FIELD23NEW * 100 + @PLUSMINSIGN,15)
					GOTO SKIPREST;


 					--exec upFORMAT_NUMBER_2_DEC @FIELD23NUM,15,1,@FIELD23 output
					select @FIELD23 = RIGHT('00000000000000000000' + CAST(CAST(@FIELD23NUM * 100 AS INT) as varchar), 15) + CASE WHEN @FIELD23NUM > 0 THEN '+' ELSE '-' END


SKIPREST:
User avatar
Ron2K
Forum Technical Administrator
Posts: 9050
Joined: 04 Jul 2006, 16:45
Location: Upper Hutt, New Zealand
Contact:

Post by Ron2K »

SBSP wrote:How can i convert nulls to 0's as in zeros ? if it finds a null value

Code: Select all

ISNULL(check_expression, replacement_value)
Kia kaha, Kia māia, Kia manawanui.
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

Thats weird cus

just above the code i have its been converted

Code: Select all

@FIELD23NUM = SUM(ISNULL(TOTALCOST,0)) 
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

Aha wait in the second IF statement i forgot to say

set @FIELD23NUMNEW = @FIELD23NUM

Code: Select all

             IF @PLUSMINSIGN = '+' AND @FIELD23NUM < 1
               set @FIELD23NEW = CAST(@FIELD23NUMNEW AS varchar)   
               select @FIELD23 =   RIGHT('00000000000000000000' +  @FIELD23NEW * 100 +
instead of

Code: Select all

             IF @PLUSMINSIGN = '+' AND @FIELD23NUM < 1
             set @FIELD23NUMNEW  = @FIELD23NUM               
             set @FIELD23NEW = CAST(@FIELD23NUMNEW AS varchar)   
             select @FIELD23 =   RIGHT('00000000000000000000' +                  @FIELD23NEW * 100 +
:roll:


Leme try again :wink:


Nha this sucks (This GW Basic style coding dont work) :lol:

I'm giving up will give it to someone that knows how.
Wasted 2 days now :evil:
FTB_Screamer
Registered User
Posts: 424
Joined: 28 Jun 2006, 02:00
Location: Johannesburg
Contact:

Post by FTB_Screamer »

viceroy wrote:you think? Good luck to you then

doesn't always
shap!!
viceroy
Registered User
Posts: 3565
Joined: 27 Mar 2006, 02:00
Location: I forget

Post by viceroy »

FTB_Screamer wrote:
viceroy wrote:you think? Good luck to you then

doesn't always
shap!!
:?:
Image
Post Reply