[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

SQL help with Subtraction

DG

3/27/2007 3:26:00 PM

I am VERY new to SQL programming and don't know if this is the correct place
for this post but here goes.

I have a spreadsheet in excel that I use ODBC to pull data from an SQL
database. I can do the basic stuff (select fields from a table and return
them). Normally I return the data to the spreadsheet then put functions
into the spreadsheet to do some basic calculations. I found some info on
doing these types of functions within the SQL statement but can't find how
to subtract two fields and return the difference.

Example the record contains Item_Code, Qty_on_Hand, Qty_Allocated. Is there
a way to return the difference between Qty_on_Hand and Qty_Allocated as
Qty_Free?

SELECT Item_Code, SUBTRACT(Qty_on_Hand, Qty_Allocated) AS 'Qty_Free' did
not work. It says Subtract is not known.

Any help would be great.

DG


5 Answers

Tibor Karaszi

3/27/2007 3:37:00 PM

0

> I found some info on doing these types of functions within the SQL statement but can't find how
> to subtract two fields and return the difference.

SELECT col1 - col2 AS colAliasname, ...
FROM tbl

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/d...
http://www.solidqualitylea...


"DG" <nothere@somewhere.com> wrote in message news:OpluuQIcHHA.2120@TK2MSFTNGP03.phx.gbl...
>I am VERY new to SQL programming and don't know if this is the correct place for this post but here
>goes.
>
> I have a spreadsheet in excel that I use ODBC to pull data from an SQL database. I can do the
> basic stuff (select fields from a table and return them). Normally I return the data to the
> spreadsheet then put functions into the spreadsheet to do some basic calculations. I found some
> info on doing these types of functions within the SQL statement but can't find how to subtract two
> fields and return the difference.
>
> Example the record contains Item_Code, Qty_on_Hand, Qty_Allocated. Is there a way to return the
> difference between Qty_on_Hand and Qty_Allocated as Qty_Free?
>
> SELECT Item_Code, SUBTRACT(Qty_on_Hand, Qty_Allocated) AS 'Qty_Free' did not work. It says
> Subtract is not known.
>
> Any help would be great.
>
> DG
>


Jack Vamvas

3/27/2007 3:39:00 PM

0

DECLARE @p1 INT,@p2 INT
SET @p1 = 7
SET @p2 = 3

SELECT @p1 - @p2

--

Jack Vamvas
___________________________________
Advertise your IT vacancies for free at - http://www.ITj...



"DG" <nothere@somewhere.com> wrote in message
news:OpluuQIcHHA.2120@TK2MSFTNGP03.phx.gbl...
>I am VERY new to SQL programming and don't know if this is the correct
>place for this post but here goes.
>
> I have a spreadsheet in excel that I use ODBC to pull data from an SQL
> database. I can do the basic stuff (select fields from a table and return
> them). Normally I return the data to the spreadsheet then put functions
> into the spreadsheet to do some basic calculations. I found some info on
> doing these types of functions within the SQL statement but can't find how
> to subtract two fields and return the difference.
>
> Example the record contains Item_Code, Qty_on_Hand, Qty_Allocated. Is
> there a way to return the difference between Qty_on_Hand and Qty_Allocated
> as Qty_Free?
>
> SELECT Item_Code, SUBTRACT(Qty_on_Hand, Qty_Allocated) AS 'Qty_Free' did
> not work. It says Subtract is not known.
>
> Any help would be great.
>
> DG
>


Raymond D'Anjou

3/27/2007 4:35:00 PM

0

SELECT Item_Code, Qty_on_Hand - Qty_Allocated AS 'Qty_Free'

"DG" <nothere@somewhere.com> wrote in message
news:OpluuQIcHHA.2120@TK2MSFTNGP03.phx.gbl...
>I am VERY new to SQL programming and don't know if this is the correct
>place for this post but here goes.
>
> I have a spreadsheet in excel that I use ODBC to pull data from an SQL
> database. I can do the basic stuff (select fields from a table and return
> them). Normally I return the data to the spreadsheet then put functions
> into the spreadsheet to do some basic calculations. I found some info on
> doing these types of functions within the SQL statement but can't find how
> to subtract two fields and return the difference.
>
> Example the record contains Item_Code, Qty_on_Hand, Qty_Allocated. Is
> there a way to return the difference between Qty_on_Hand and Qty_Allocated
> as Qty_Free?
>
> SELECT Item_Code, SUBTRACT(Qty_on_Hand, Qty_Allocated) AS 'Qty_Free' did
> not work. It says Subtract is not known.
>
> Any help would be great.
>
> DG
>


DG

3/27/2007 5:06:00 PM

0

Thanks. I knew it was simple, but not that simple.


"DG" <nothere@somewhere.com> wrote in message
news:OpluuQIcHHA.2120@TK2MSFTNGP03.phx.gbl...
>I am VERY new to SQL programming and don't know if this is the correct
>place for this post but here goes.
>
> I have a spreadsheet in excel that I use ODBC to pull data from an SQL
> database. I can do the basic stuff (select fields from a table and return
> them). Normally I return the data to the spreadsheet then put functions
> into the spreadsheet to do some basic calculations. I found some info on
> doing these types of functions within the SQL statement but can't find how
> to subtract two fields and return the difference.
>
> Example the record contains Item_Code, Qty_on_Hand, Qty_Allocated. Is
> there a way to return the difference between Qty_on_Hand and Qty_Allocated
> as Qty_Free?
>
> SELECT Item_Code, SUBTRACT(Qty_on_Hand, Qty_Allocated) AS 'Qty_Free' did
> not work. It says Subtract is not known.
>
> Any help would be great.
>
> DG
>


kk

3/28/2007 1:52:00 AM

0

Hi DG
Use a - (Subtract operator) like the following
SELECT Item_Code, (Qty_on_Hand - Qty_Allocated) AS 'Qty_Free' FROM TableName

--
Krishnakumar S

What lies behind you and what lies before you is nothing compared to what
lies within you


"DG" wrote:

> I am VERY new to SQL programming and don't know if this is the correct place
> for this post but here goes.
>
> I have a spreadsheet in excel that I use ODBC to pull data from an SQL
> database. I can do the basic stuff (select fields from a table and return
> them). Normally I return the data to the spreadsheet then put functions
> into the spreadsheet to do some basic calculations. I found some info on
> doing these types of functions within the SQL statement but can't find how
> to subtract two fields and return the difference.
>
> Example the record contains Item_Code, Qty_on_Hand, Qty_Allocated. Is there
> a way to return the difference between Qty_on_Hand and Qty_Allocated as
> Qty_Free?
>
> SELECT Item_Code, SUBTRACT(Qty_on_Hand, Qty_Allocated) AS 'Qty_Free' did
> not work. It says Subtract is not known.
>
> Any help would be great.
>
> DG
>
>
>