|
-
Aug 16th, 2005, 10:53 AM
#1
Thread Starter
Frenzied Member
Toggle bit field with one query [Solved Again!]
SQL Server 2K. I have a bit field. I need to toggle it. Can I do it with one query, rather than SELECTing it, and updating according to a logic evaluation?
Last edited by ober0330; Aug 16th, 2005 at 12:29 PM.
-
Aug 16th, 2005, 10:59 AM
#2
Re: Toggle bit field with one query
Just do an update query....
UPDATE SomeTable
SET MyBit = NOT MyBit
FROM SomeTable
WHERE Field = MyValue
Tg
-
Aug 16th, 2005, 11:02 AM
#3
Thread Starter
Frenzied Member
Re: Toggle bit field with one query
Hmm... why couldn't I have thought of that? Thanks!
-
Aug 16th, 2005, 11:20 AM
#4
Re: Toggle bit field with one query [Resolved]
Sometimes it's those simple easy things we over look. I chalk it up to being a hazard of being a programmer.... we tend over analyze problems.
Tg
-
Aug 16th, 2005, 12:15 PM
#5
Thread Starter
Frenzied Member
Re: Toggle bit field with one query [Not working!]
Ok... I thought it would just work, but it's not. It doesn't like the NOT keyword:
UPDATE CompHistory SET Active = NOT Active WHERE HIST_ID IN ('10532-367-A')
-
Aug 16th, 2005, 12:20 PM
#6
Re: Toggle bit field with one query [Not working!]
You know what... I bet it's because NOT wants to turn 0 to -1, which can't be handled by a bit type (0,1)....
Two things to try (and I'll try both as soon as I cna find a suitable test DB) ...
SET Active = ABS(NOT Active) ....
OR
SET Active = CASE Active WHEN 1 THEN 0 ELSE 1 END.....
Tg
-
Aug 16th, 2005, 12:20 PM
#7
Re: Toggle bit field with one query [Not working!]
How about this?
UPDATE CompHistory SET Active = (1-Active) WHERE HIST_ID IN ('10532-367-A')
-
Aug 16th, 2005, 12:23 PM
#8
Re: Toggle bit field with one query [Not working!]
OK, weird.... the CASE construct works but using NOT on the field doesn't.... seems to fly in the face of logic...
The full SQL I used to test this:
Code:
UPDATE tblQCCode
SET Enabled =CASE Enabled WHEN 1 THEN 0 ELSE 1 END
FROM tblQCCode
WHERE Description = 'Invoice Inquiry'
Tg
-
Aug 16th, 2005, 12:29 PM
#9
Thread Starter
Frenzied Member
Re: Toggle bit field with one query [Not working!]
Yeah... the CASE works, and si_the_geek's works as well, so I guess I'll take the shorter of the two. Thanks for the help fellas!
-
Aug 16th, 2005, 12:30 PM
#10
Re: Toggle bit field with one query [Solved Again!]
His is also more portable than mine....
Tg
-
Aug 16th, 2005, 12:35 PM
#11
Thread Starter
Frenzied Member
Re: Toggle bit field with one query [Solved Again!]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|