Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
GVeers
Starting Member
9 Posts |
Posted - 2010-03-12 : 16:07:48
|
| Say I have data 3 fields (columns) with the same data type. Is there an easy way to create a new field that has the maximum value of these 3 fields? It seems simple enough but I cannot for the life of me figure out how to do this with an UPDATE statement. |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-03-12 : 16:15:11
|
| [CODE]update mytableset field4 = case when field1 >= field2 and field1 >= field3 then field1 when field2 >= field1 and field2 >= field3 then field2 else field3 end[/CODE]=======================================There are no passengers on spaceship earth. We are all crew. -Marshall McLuhan, educator and philosopher (1911-1980) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-13 : 10:53:33
|
What do you mean when you say "create a new field"?Would you please come up with an example? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-15 : 03:04:47
|
quote: Originally posted by webfred What do you mean when you say "create a new field"?Would you please come up with an example? No, you're never too old to Yak'n'Roll if you're too young to die.
I think it is a computed columnMadhivananFailing to plan is Planning to fail |
 |
|
|
GVeers
Starting Member
9 Posts |
Posted - 2010-03-15 : 09:25:55
|
| madhivanan, as usual, speaks the truth. :)Thanks Bustaz - I was trying to avoid a multiple if-then statement since it seems so clunky. But if that's the way to do it, then that's the way. I appreciate it! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-15 : 10:25:20
|
quote: Originally posted by GVeers madhivanan, as usual, speaks the truth. :)Thanks Bustaz - I was trying to avoid a multiple if-then statement since it seems so clunky. But if that's the way to do it, then that's the way. I appreciate it!
another way is there which is by unpivoting and getting them to rows and then finding max and pivoting them back. but if the number of columns involved is small i prefer the suggested case when approach------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|