adding two columns in sql server ?

  • hi

    i have a table called "finance" and these columns

    date                amt_recharge          amt_food          total

    12/2/2005             50                         20                 70

    how can i write a query in the sql server enterprise manager to add up both the columns "amt_recharge" and "amt_food" and the resultant output in the "total" ? And by the way where and how do i write this code in the sql .....or should i write it in the vb form?

    thanks

  • You can create table like this

    CREATE TABLE [dbo].[t1] (

     [date] [smalldatetimer], [amt_recharge] [int] NULL ,

     [amt_food ] [int] NULL ,

     [bc] AS ([amt_recharge] + [amt_food])

    ) ON [PRIMARY]

    GO




    My Blog: http://dineshasanka.spaces.live.com/

  • SELECT Total = ISNULL(amt_recharge,0) + ISNULL(amt_food.0) FROM ....

    You don't need a total column at all. It's redundant.

    You can write this code in Query Analyzer, which you will find in the SQL Server programm group or ISQLW.EXE when started from the command line. Just make sure you point to the appropriate database.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • the reason why i want it in the table is because when i display it in the vb.net form as a datagrid ... i wanted to see the total as a column which displays each of them ......or is there any other way to do this ... and i also want to total of the total column below ...

  • i did something else like

    CREATE TABLE [dbo].[t1] (

     [date] [smalldatetimer], [amt_recharge] [int] NULL ,

     [amt_food ] [int] NULL ,

     [bc] AS ([amt_recharge] + [amt_food])

    ) ON [PRIMARY]

    GO

    in the sql query analyser and it saves it as an object or something like that but i dont know how to retrieve that and it is not seen in the users table ....

  • smalldatetimer

    This is likely to be a typo.

    CREATE TABLE [dbo].[t1]

    (

     [date] [smalldatetime], [amt_recharge] [int] NULL ,

     [amt_food ] [int] NULL ,

     [bc] AS ([amt_recharge] + [amt_food])

    ) ON [PRIMARY]

    GO

    INSERT INTO t1 VALUES('20050212',50,20)

    SELECT * FROM t1

    DROP TABLE t1

    date                                                   amt_recharge amt_food    bc         

    ------------------------------------------------------ ------------ ----------- -----------

    2005-02-12 00:00:00                                    50           20          70

    (1 row(s) affected)

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • hey cool it works ....... but the problem being that..... i cant retrieve the table t1 from  vb.net ... its showing all the tables except the "t1" table

  • IT works ... thanx a lot dinesh and frank

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply