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?

Alexander
  • 19,906
  • 19
  • 75
  • 162

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,713
  • 12
  • 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,486
  • 4
  • 32
  • 48
-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
  • 438,706
  • 87
  • 741
  • 845
  • Strictly speaking `0`and `1` are **not** boolean values - at least not in the SQL standard. But as SQL Server does not support boolean values this is a workaround. –  Jan 16 '18 at 08:25
  • A tip, do not repeat the Question in the Answer, just the answer is enough. – Peter B Jan 16 '18 at 08:28
  • What relevance does the youtube link have? I assume this is yours and you are trying to spam it? – Martin Smith Jan 16 '18 at 08:36