-1

In a SQL Server table, I have a BIT column and based its value, I need to update that table's other columns with some values. I tried this

UPDATE tablename SET Completed = GETDATE() WHERE CheckTaskAvailable = TRUE

but I get the error

Msg 207, Level 16, State 1, Server SQLDEV01, Line 1
Invalid column name 'TRUE'.

How to do this in a T-SQL query?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Tarun
  • 25
  • 1
  • 6

3 Answers3

4

if you want to set as true try

Update table set columnName = 1 where ...

if you want to set as false try

Update table set columnName = 0 where ...
Reza
  • 18,865
  • 13
  • 88
  • 163
2

In addition to using the values 0 and 1, the T-SQL documentation for bit says that

The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.

so these work, too:

UPDATE tablename SET bitcolumn = 'TRUE' WHERE ...

UPDATE tablename SET othercolumn = 'something' WHERE bitcolumn = 'TRUE'
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0

I had a need to do something similar where I was looking to update a field based on whether a record existed or not existed in another table so I used above code (thank you RezaRahmati) and added:

Update table set columnName = 1 where ID IN (SELECT ID FROM other table)

or for false

Update table set columnName = 0 where ID NOT IN (SELECT ID FROM other table)

I really enjoy Stack Overflow it has really helped me learn.