如何在SQL Server 2005中的运算符与case语句之间使用

时间:2010-12-07 08:54:25

标签: sql-server-2005

如何将BETWEEN运算符与CASE语句

一起使用
@FromEmpAge
@ToempAge

select * from Emp
where EmpAge between
  case when ToempAge0 then @FromEmpAge and @ToempAge end

怎么做?

5 个答案:

答案 0 :(得分:1)

不确定它是否有效 - 如果确实如此,您必须确保首先正确完成CASE声明!

SELECT 
    (list of columns) 
FROM 
    dbo.Emp
WHERE
    EmpAge BETWEEN 
      CASE (some column)                     | this is the first value 
          WHEN ToEmpAge0 THEN @FromEmpAge    | for the BETWEEN ....
      END                                    | close the CASE
      AND @ToempAge                          | this is the second value

CASE需要至少有一个WHEN....THEN....然后END才能“关闭”然后CASE - 只有在此之后,您才能继续...... ...

但又一次:不确定这是否有效,即使采用“正确”的语法......

答案 1 :(得分:0)

我不确定你对ToEmpAge0的意思。无论如何, marc_s 已回答了有关如何在CASE运算符中使用BETWEEN语句的问题,如果您在WHEN之后放置了正确的文字值,该问题就会奏效。 您还可以将CASE语句用于BETWEEN运算符的第二个操作数。

我将假设ToEmpAgeINT

的列
SELECT 
    (list of columns) 
FROM 
    dbo.Emp
WHERE
    EmpAge BETWEEN 
       CASE ToEmpAge 
          WHEN 0 THEN @FromEmpAge
          WHEN 1 THEN @FromEmpAge+10 --or whatever
       END
    AND
       CASE ToEmpAge
          WHEN 0 THEN @ToEmpAge
          WHEN 1 THEN @ToEmpAge+5
       END

答案 2 :(得分:0)

        SELECT *
        FROM   CTE
        WHERE   RN BETWEEN case @Excel when 0 then (@StartRow - cast(@NumberOfRows AS int)) else @NumberOfRows end
                   AND case @Excel when 0 then (@StartRow - 1) else @PageIndex end

答案 3 :(得分:0)

    DECLARE @StartRow INT
    SET @StartRow = ( cast(@PageIndex as int) * cast(@NumberOfRows as int) ) + 1 ;


    WITH    CTE
              AS ( SELECT   ROW_NUMBER() OVER ( ORDER BY CASE
                                                          WHEN @SortColumnName = 'ID'
                                                          AND @SortOrderBy = 'asc'
                                                          THEN sod.ID
                                                          END ASC, CASE
                                                          WHEN @SortColumnName = 'ID'
                                                          AND @SortOrderBy = 'desc'
                                                          THEN sod.ID
                                                          END DESC, CASE
                                                          WHEN @SortColumnName = 'MessageText'
                                                          AND @SortOrderBy = 'asc'
                                                          THEN MessageText
                                                          END ASC, CASE
                                                          WHEN @SortColumnName = 'MessageText'
                                                          AND @SortOrderBy = 'desc'
                                                          THEN MessageText
                                                          END DESC, CASE
                                                          WHEN @SortColumnName = 'TO'
                                                          AND @SortOrderBy = 'desc'
                                                          THEN [TO]
                                                          END DESC
                                                           ) AS RN ,
   [SendedDate]
  ,[UserID]
  ,sod.[ID]
  ,[SmsOutboxID]
  ,[MessageID]
  ,[FolderID]
  ,[From]
  ,[TO]
  ,[MessageText]
  ,[SendedType]
  ,[SendedStatus]
  ,[IsDelete]
  ,[NumberOfMessage]
  ,[MessageType]
  ,[PricesbySMS]
  ,[Sended]


   FROM SMS_Outbox so inner join SMS_OutboxDetails sod on so.ID = sod.SmsOutboxID where so.UserID = @UserID and sod.SendedType = @Type and sod.IsDelete = 0)

        SELECT *
        FROM   CTE
        WHERE   RN BETWEEN case @Excel when 0 then (@StartRow - cast(@NumberOfRows AS int)) else @NumberOfRows end
                   AND case @Excel when 0 then (@StartRow - 1) else @PageIndex end

答案 4 :(得分:0)

编写一个查询,显示员工的姓名,首字母大写,所有其他字母小写,部门号。对于名称中第二个字符位于“A”和“M”之间的所有员工,请为每列添加适当的标签。 我尝试了一些不起作用的东西。我的工作代码是:

SELECT INITCAP(ename), deptno 
FROM  emp
WHERE ename LIKE BETWEEN ('_A%' AND '_M%');