If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > how to truncate just a column ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-13-11, 00:54
navedjobs navedjobs is offline
Registered User
 
Join Date: Sep 2011
Posts: 35
how to truncate just a column ?

Code:
credit | debit | cash_in_hand
------------------------------
100      100        0
consider this as my table where i have an requirement
where i need to truncate just the column cash_in_hand
but if i do something like this
Code:
delete from credit_debit_table where cash_in_hand = '0'
then it will delete the entire row with credit & debit information
but i need to delete only cash_in_hand column how to do that ?

note : i need to delete only data not the structure of cash_in_hand
Reply With Quote
  #2 (permalink)  
Old 12-13-11, 01:05
baburajv baburajv is offline
Registered User
 
Join Date: Feb 2004
Location: Bangalore, India
Posts: 242
update table set cash_in_hand = NULL ???
__________________
Cheers....

baburajv
Reply With Quote
  #3 (permalink)  
Old 12-13-11, 03:14
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
SQL Server (and all the RDBMSs too) stores data on a row-basis. This means that your requirement does not have any sense. You can put the column data to NULL as already suggested, but this is just a data approach.
Unless your requirement is about storage space.
__________________
Florin Aparaschivei
Iasi, Romania
Reply With Quote
  #4 (permalink)  
Old 12-13-11, 12:17
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Quote:
Originally Posted by navedjobs View Post
note : i need to delete only data not the structure of cash_in_hand
can you explain what you want to happen in terms of the real world as opposed to the database? What you are describing resembles "I want to paint that rain cloud red." which may be possible under some circumstances, but doesn't make much sense and seems like it would require far more effort than it might justify.

If we could understand what you're trying to accomplish, we can probably offer you a good answer. Based on your description of your problem so far, baburajv has offered the best answer I can imagine.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #5 (permalink)  
Old 12-14-11, 10:34
weejas weejas is offline
Registered User
 
Join Date: Sep 2006
Location: Surrey, UK
Posts: 448
A variable value like cash in hand ought not to be stored in a table. Create a view to show this as the difference between the credit and debit values, and that way you will always have the current value available.
__________________
10% of magic is knowing something that no-one else does. The rest is misdirection.
Reply With Quote
  #6 (permalink)  
Old 12-19-11, 13:53
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
weejas, that can be a very expensive query to run time after time. I'm all for dynamically calculating when possible, but performing the same expensive calculations time after time on historical data, always getting the exact same result, begs for storing the data in the row.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 12-19-11, 14:56
weejas weejas is offline
Registered User
 
Join Date: Sep 2006
Location: Surrey, UK
Posts: 448
This is true. However, it depends on how many transactions there are and how many times the information is required on a daily basis as to whether the performance hit will be a deal-breaker. For example, I work with two ERPSs, and neither of them store the stock on hand in the inventory.

Also, whether you're running a query over the whole table to sum transactions for display or running a query over the whole table to sum transactions for storage, you're still running the query over the same table.
__________________
10% of magic is knowing something that no-one else does. The rest is misdirection.
Reply With Quote
  #8 (permalink)  
Old 12-20-11, 15:33
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
Quote:
Originally Posted by weejas View Post
Also, whether you're running a query over the whole table to sum transactions for display or running a query over the whole table to sum transactions for storage, you're still running the query over the same table.
Do I need to point out that you run it only once for storage, and it can be performed during off-hours?
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #9 (permalink)  
Old 12-21-11, 04:17
weejas weejas is offline
Registered User
 
Join Date: Sep 2006
Location: Surrey, UK
Posts: 448
And do I need to point out that if you run such a query overnight, your balances will not reflect any transactions since then? :P

There are pros and cons to both approaches. I favour calculating such totals as and when they are required, but I accept that it is possible to store them too, and in some cases is preferable.
__________________
10% of magic is knowing something that no-one else does. The rest is misdirection.
Reply With Quote
  #10 (permalink)  
Old 01-09-12, 12:54
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
Apparently I need to point out that it need be run across the table only once, for existing data.
A trigger or procedure code can keep individual records updated, very efficiently.
Apparently, that needed to be pointed out.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #11 (permalink)  
Old 01-09-12, 16:18
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Quote:
Originally Posted by baburajv View Post
update table set cash_in_hand = NULL ???
ding...ding...ding...ding....we have a winner

Johnny, tell'em what he's won
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #12 (permalink)  
Old 01-10-12, 12:49
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
Dong...dong...dong...dong...dong!
That answered his question, but did not solve his problem.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #13 (permalink)  
Old 01-10-12, 13:00
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Quote:
Originally Posted by blindman View Post
Dong...dong...dong...dong...dong!
That answered his question, but did not solve his problem.
Where did Oppie say that?
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #14 (permalink)  
Old 01-11-12, 12:07
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
Brett, read the post history.
The problem is how best to handle a calculated value such as cash in hand.
The discussion was whether CashInHand should be stored in the table at all.
This is clearly pertinent to both the original poster, and others who encounter this thread with similar problems.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On