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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Quick help please

Author  Topic 

cjdsql
Starting Member

2 Posts

Posted - 2011-03-23 : 15:49:40
I'm looking for a way to replace one column in a view with the values of another column in the same view based on the conditions of the first column.

My view already creates something like this:

field1 field2 field3 field4
123 456 abc xyz
789 234 qwe rty
123 578 wte pop

I need to replace the values in field1 with the values in field2 where field1 equals 123. And only if it equals 123. Kind of on a time crunch if anyone can help. THANKS!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-23 : 15:53:44
UPDATE Table1
SET field1 = field2
WHERE field1 = 123

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cjdsql
Starting Member

2 Posts

Posted - 2011-03-23 : 16:06:20
quote:
Originally posted by tkizer

UPDATE Table1
SET field1 = field2
WHERE field1 = 123

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Thanks for the quick reply, but this information is not in a table. The view is created from a table which has all of it information in 1 field. The view breaks it up into multiple fields. It is at this point (in the creation of the view) that I would like to do the comparison and replacement. There are several other queries that run against this 1 view. So if I fix it, I fix everything in one place.

Table1 looks like this:

FIELD1
123456abcxyz

The view breaks in into:

Field1 Field2 Field3 Field4
123 456 abc xyz

Then I need to compare and replace. Maybe that explains it a little better.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-03-23 : 16:48:58
SELECT CASE WHEN field1=123 THEN field2 ELSE field1 END
FROM myView
Go to Top of Page
   

- Advertisement -