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)
 update bit column

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2008-08-27 : 02:20:34
I have a table with a bit column, in some cases that value is true and in some false, can I in one single update change the ones that are true to false and vice versa?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-27 : 02:24:17
yes. use CASE WHEN to do it


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2008-08-27 : 02:39:49
Ahhh, thanks alot!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-27 : 02:43:29
[code]DECLARE @Sample TABLE (r INT IDENTITY(100, 10), b BIT)

INSERT @Sample
SELECT 0 UNION ALL
SELECT 1

SELECT *
FROM @Sample

UPDATE @Sample
SET b = 1 - b

SELECT *
FROM @Sample[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-27 : 03:11:21
[code]DECLARE @Sample TABLE (r INT IDENTITY(100, 10), b BIT)

INSERT @Sample
SELECT 0 UNION ALL
SELECT 1

SELECT *
FROM @Sample

UPDATE @Sample
SET b = b ^ 1

SELECT *
FROM @Sample[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

joe.inom
Starting Member

9 Posts

Posted - 2008-08-27 : 04:22:58
yes you can do that , but keep in mind that the codecs are to be perfect.


San Diego Home Insurance

joe Alexender
Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2008-08-27 : 04:34:34
Hi Peso

I see that your sugestion works just fine, but can you explain the "SET b = b ^ 1" part?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-27 : 04:45:11
its exclusive OR operator.have a look at BOL for details

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/f38f0ad4-46d0-40ea-9851-0f928fda5293.htm
Go to Top of Page
   

- Advertisement -