5

How do I implement boolean logic in the select statement while the query is running?

SELECT t.[Key]
      ,t.[Parent_Key]
      ,t.[Parent_Code]
      ,t.[Code]
      ,t.[Desc] 
      ,t.[Point]
      ,[isChild] -- If Point > 2, then true, if Point == 1 Then false   
      ,t.[By] 
      ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

I want make some logic to determine [isChild] boolean value based on t.[Point]

1
  • CASE Commented Jul 11, 2014 at 14:39

4 Answers 4

5
SELECT t.[Key]
      ,t.[Parent_Key]
      ,t.[Parent_Code]
      ,t.[Code]
      ,t.[Desc] 
      ,t.[Point]
      ,CASE WHEN t.[Point] > 2 THEN 1 ELSE  
            CASE WHEN t.[Point] = 1 THEN 0 ELSE NULL END 
       END AS [isChild]
      ,t.[By] 
      ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

Be aware that when t.[Point] < 1 then [isChild] will be null

Sign up to request clarification or add additional context in comments.

Comments

1

Case is your friend...

 SELECT Key, Parent_Key, Parent_Code, Code, Desc, point, 
     case when point > 2 then 1 
          when point = 1 then 0 end isChild, 
     [By], [On]
 FROM db.stats  
 WHERE Parent_Key= @tmpParameter

Comments

0

Use the case statement:

SELECT t.[Key]
      ,t.[Parent_Key]
      ,t.[Parent_Code]
      ,t.[Code]
      ,t.[Desc] 
      ,t.[Point]
      ,CASE t.[Point] WHEN 1 THEN FALSE WHEN 2 THEN TRUE END as [isChild]
      ,t.[By] 
      ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

Comments

0

You can use CASE statement

SELECT t.[Key]
  ,t.[Parent_Key]
  ,t.[Parent_Code]
  ,t.[Code]
  ,t.[Desc] 
  ,t.[Point]
  ,CASE 
  WHEN t.[Point] THEN true 
  else false
  END as isChild
  ,t.[By] 
  ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.