0

I need to get a boolean field in a view from a string field in the table. The boolean field should be True if the string is filled:

SELECT (MasterId IS NOT NULL) AS HasMaster FROM entries

However, this causes the error:

incorrect syntax near the keyword 'IS'

Why is that and how to fix?

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Alexander
  • 19,443
  • 17
  • 61
  • 146

3 Answers3

5

Try the following code:

SELECT CAST(CASE WHEN MasterId IS NULL THEN 0 ELSE 1 END AS BIT) [HasMaster]
FROM entries
sanatsathyan
  • 1,553
  • 10
  • 18
1

Try this,

SELECT (CASE WHEN MasterId IS NOT NULL THEN 1 ELSE 0 END) AS HasMaster 
FROM  entries

There is no Boolean values in SQL Server, so you can use '0' and '1' in CASE expression here.

Abdul Rasheed
  • 6,102
  • 4
  • 32
  • 47
-1

Question: I need to get a boolean field in a view from a string field in the table. The boolean field should be True if the string is filled

Answer: use case

select case when MasterId is null then 0 else 1 end
from table_name
Martin Smith
  • 419,657
  • 83
  • 708
  • 800