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 |
|
bobshishka
Yak Posting Veteran
72 Posts |
Posted - 2007-11-07 : 12:26:56
|
| I have a table named 'Personal' that has a field that contains values that represent colors (ex: BLACK, BROWN, BLUE, etc.). How do I run an update statement to update the values to defined set. For example (BLACK = BLK, BLUE = BLU, etc.)?Thanks |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-11-07 : 12:33:57
|
| [code]UPDATE PersonalSET 'BLACK' = 'BLK' ,'BLUE' = 'BLU'[/code]etc[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-07 : 12:36:17
|
wouldn't it beUPDATE PersonalSET [ColumnName] = (Case when 'BLACK' then 'BLK' when 'BROWN' then 'BRN' --etc END) |
 |
|
|
bobshishka
Yak Posting Veteran
72 Posts |
Posted - 2007-11-07 : 12:41:04
|
| Using UPDATE PersonSET [EYE_COLOR] = (Case when 'BLACK' then 'BLK' when 'BROWN' then 'BRN' END)I get this error:Server: Msg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'then'. |
 |
|
|
bobshishka
Yak Posting Veteran
72 Posts |
Posted - 2007-11-07 : 12:53:08
|
| OK, I got this to work:UPDATE PersonSET HAIR_COLOR = 'BLK'WHERE HAIR_COLOR = 'BLACK'Now, how do I change multiple values with one command? |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-07 : 13:58:24
|
| Oops my bad[code]UPDATE PersonSET [HAIR_COLOR] = (Case [HAIR COLOR] when 'BLACK' then 'BLK' when 'BROWN' then 'BRN' --etcEND)OR[CODE]UPDATE PersonSET [HAIR_COLOR] = (Case when HAIR_COLOR = 'BLACK' then 'BLK' when HAIR_COLOR = 'BROWN' then 'BRN' --etcEND)[/CODE] |
 |
|
|
|
|
|
|
|