Sql server 2008 Select语句中的布尔逻辑

Sql server 2008 Select语句中的布尔逻辑,sql-server-2008,sql,Sql Server 2008,Sql,在查询运行时,如何在select语句中实现布尔逻辑 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]

在查询运行时,如何在select语句中实现布尔逻辑

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

我想根据t.[Point]

使用case语句来确定[isChild]布尔值:

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

您可以使用CASE语句

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
凯斯是你的朋友

 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
请注意,当t.[Point]<1时,[isChild]将为null

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