views:

901

answers:

4

How do I update different columns and rows across a table? I want to do something similiar to replace a string in SQL server

I want to do this but the value exists in multiple columns of the same type. The values are foreign keys varchars to an employee table. Each column represents a task, so the same employee may be assigned to several tasks in a record and those tasks will vary between records. How can I do this effectively? Basically something of a replace all accross varying columns throughout a table.

Thanks for any help or advice.

Cheers, ~ck in San Diego

A: 

This should do the trick:

UPDATE table1
SET field1 = replace(field1, 'oldstring', 'newstring'),
    field2 = replace(field2, 'oldstring2', 'newstring2')

etc...

Lance Roberts
and its ok if the string doesn't exist in the column? ok this sounds cool. I would just apply it accross all possible columns??? Thanks for the tip. i will try this. Sweet!
Hcabnettek
Please don't try it right out on the production system (or at least do a backup first).
Lance Roberts
Also, you can use a where clause to narrow things down a lot (probably).
Lance Roberts
no lance, this is definitely a dev database. lol
Hcabnettek
Just to update any interested parties; I ran into a bit of an issue with this. The datatype is varchar(7), if 'newstring' is same length or shorter as 'oldstring' all is well, but when I was trying to go the other direction, data will be truncated errors reared their ugly head. A less effecient way came to mind and that was to run an update on each column in the stored proc like Begin Update table1 SET field1='newstring' WHERE field1='oldstring' END, then do the same query for field2, field3, filedn. I am sure it is not effecient, but the result is what I was seeking. Thanks all!
Hcabnettek
A: 

For SQL Server 2005 this should do the trick:

http://www.mssqltips.com/tip.asp?tip=1555

Allows searching and replacing across all columns across all tables. Make sure you read the article though.

Arj
A: 

In answer to the poster's supplementary question about how to normalize this data structure. Here's how you'd do it:

Project
-------
ProjectID
ProjectName
etc...

Employee
--------
EmployeeID
EmployeeName
etc...

Task
----
TaskID
ProjectID
EmployeeID
TaskDescription
etc...

Your current structure, where you have a bunch of Task1, Task2, etc... columns in the Project table, was clearly not designed by somebody that understands relational databases.

During the process of firing that individual, you might explain that his design violates the First Normal Form, while directing him to the "Repeating groups across columns" section of that linked article.

Jason Kester
lol ridiculous. No one should be fired because they design a database a little differently or slightly denormalized. Learning from someone more experienced is helpful. Besides she, not he. is a superhottie pinay. :)
Hcabnettek
Ah, then all bets are off :) For what it's worth though, this is a much bigger deal than "a little denormalized". It's the First Normal Form that's being violated, not the 3rd.There are circumstances that call for flattening tables in a way that duplicates data, but that's not what's happening here. This is just pain generation for no benefit.
Jason Kester
A: 

I want to use case statements in sql server 2008 using case or if else statements in the code below Any help would be greatly appreciated.

SELECT [Ip Pop].[Appl Id], [Ip Pop].[Account Number], [Ip Pop].[Rel Type Desc], [Ip Pop].[Relationship Desc]

FROM [Ip Pop]

if (Instr (Relationship Desc,'Trust', 0, 1)

||(Instr (Relationship Desc,'UTMA', 0, 1)

||(Instr (Relationship Desc,'Estate', 0, 1)

||(Instr (Relationship Desc,'Exect', 0, 1)

||(Instr (Relationship Desc,'Escrow', 0, 1)

||(Instr (Relationship Desc,'IOLTA', 0, 1)

||(Instr (Relationship Desc,'Tenant', 0, 1)) {"TUA"}

elseif|(Instr (Relationship Desc,'Joint', 0, 1)) {'JOD'}

elseif|(Instr (Relationship Desc,'Primary', 0, 1)

||(Instr (Relationship Desc,'Minor', 0, 1)

||(Instr (Relationship Desc,'Sole', 0, 1)

||(Instr (Relationship Desc,'HSI', 0, 1){"IND"}

elseif(Instr (Relationship Desc,'Corp', 0, 1)

||(Instr (Relationship Desc,'Part', 0, 1)

||(Instr (Relationship Desc,'Busi', 0, 1)

||(Instr (Relationship Desc,'Comp', 0, 1)

||(Instr (Relationship Desc,'DBA', 0, 1)

||(Instr (Relationship Desc,'Nonp', 0, 1)

||(Instr (Relationship Desc,'Limited', 0, 1)

||(Instr (Relationship Desc,'Non-P', 0, 1)

||(Instr (Relationship Desc,'Club', 0, 1){"COA"}

elseif(Instr (Relationship Desc,'Cust', 0, 1)){"CSA"}

elseif(Instr (Relationship Desc,"IRA", 0, 1)){"IRA"}

else{""}

veena