October 22, 2011 at 2:21 pm
Comments posted to this topic are about the item Update table
October 22, 2011 at 2:21 pm
October 23, 2011 at 10:59 am
I didn't test the answer but I knew the following:
#2: UNION is not allowed to be used that way;
#3: That's not enough and will make things worse as every col1 will have the same value;
#4: We know that nothing is impossible. 😀
Got it right by elimination.
Thank you for the question.
Best regards,
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
October 23, 2011 at 8:53 pm
UPDATE table1 SET col1 = (select distinct case a.col1 when '1' then '0' else '1' end from table1 a where a.col1=table1.col1)
The solution is very clever!
October 23, 2011 at 9:45 pm
I understand that the answer works, but can someone explain the process for it?
Is it:
Join the original table with a derived table, using CASE to swap the values (which occurs after the join condition), DISTINCT for some reason and then UPDATE using the correlated subquery?
October 23, 2011 at 10:41 pm
Oh, boy! I hope no one will use this code in production! :blink:
October 23, 2011 at 11:09 pm
How about...
Update table1 set col1 = col1^1
October 23, 2011 at 11:41 pm
"Update table1 set col1 = col1^1" is really great one as it is also work for table variable.
October 23, 2011 at 11:52 pm
The solution seems a bit opaque. Something like this is (to me) much more straightforward for the next person who has to read it:
update #t
set col1 = case col1 when 1 then 0
when 0 then 1
else col1
end
October 24, 2011 at 12:19 am
xiewei (10/23/2011)
UPDATE table1 SET col1 = (select distinct case a.col1 when '1' then '0' else '1' end from table1 a where a.col1=table1.col1)The solution is very clever!
No its not. Its infact really bad since its ALOT more then whats needed to do this operation. There are several examples below which are alot better.
/T
October 24, 2011 at 1:03 am
CELKO (10/23/2011)
UPDATE Table1SET col1 = -(col1 - 1);
Why make it so insanely complex? Here is a good programming exercise. Given table with {-1, 0, +1} in a column, write expressions to yield all possible mapping of the set to the three values. For exampolke
{+1, 0, -1}
is
UPDATE Table1
SET col1 = -col1 ;
Excellent solution! Much more efficient.
/Håkan Winther
MCITP:Database Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
October 24, 2011 at 1:06 am
charlietuna (10/23/2011)
The solution seems a bit opaque. Something like this is (to me) much more straightforward for the next person who has to read it:update #t
set col1 = case col1 when 1 then 0
when 0 then 1
else col1
end
I agree!
UPDATE table1 SET col1 = case col1 when '1' then '0' else '1' end
KISS: Keep It Stupid & Simple!
October 24, 2011 at 1:06 am
charlietuna (10/23/2011)
The solution seems a bit opaque. Something like this is (to me) much more straightforward for the next person who has to read it:update #t
set col1 = case col1 when 1 then 0
when 0 then 1
else col1
end
Another solution that is much more efficient than the correlated subquery in the question! 🙂 Personally I didn't vote for the "correct answer" because I was hoping it didn't work.
/Håkan Winther
MCITP:Database Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
October 24, 2011 at 2:03 am
As an example of a bit flip flop it fails on so many levels, but as an example of using case in an update statement it works for me.
Good question, cheers.
_____________________________________________________________________
[font="Comic Sans MS"]"The difficult tasks we do immediately, the impossible takes a little longer"[/font]
October 24, 2011 at 2:11 am
This was removed by the editor as SPAM
Viewing 15 posts - 1 through 15 (of 76 total)
You must be logged in to reply to this topic. Login to reply