不必要的转换时出现Case语句错误

时间:2014-10-24 13:21:36

标签: sql sql-server tsql sql-server-2014

我目前正在为一个视图编写脚本,如下所示,但我在最后五次选择中遇到了一些问题。我从一个主要是int值的表中提取但包含一些NULLS。我喜欢的是这些' rep'的任何NULL值。要输出的列为“N / A”和“#”;但是,case语句中的某些逻辑会在尝试将N / A转换为INT时抛出错误。

我认为在case语句中的某个位置,它将NULLS设置为N / A,然后尝试将值添加到INT中,但是会对它可能会失败的地方感到一些帮助。

非常感谢!

SELECT
--Name
ExerciseCultures.Name,
--Weight
CASE
  WHEN weight IS NULL THEN '0'
  ELSE
    CASE
      WHEN Type = 'Isolation' THEN
        CASE
          WHEN reps1 < '12'
               AND reps2 < '12'
               AND reps3 < '12' THEN weight
          ELSE
            CASE
              WHEN exercisecultures.name IN ( 'Hammer Curls', 'Concentration Curls' ) THEN workoutsessions.weight + ( 2.0 )
              ELSE WorkoutSessions.weight + ( 2.5 )
            END
        END
      ELSE
        CASE
          WHEN Type = 'Compound' THEN
            CASE
              WHEN name = 'Bench Press' THEN
                CASE
                  WHEN reps1 < '3'
                       AND reps2 < '3'
                       AND reps3 < '3' THEN weight
                  ELSE workoutsessions.weight + ( 2.5 )
                END
              ELSE
                CASE
                  WHEN reps1 < '5'
                       AND reps2 < '5'
                       AND reps3 < '5' THEN weight
                  ELSE workoutsessions.weight + ( 2.5 )
                END
            END
        END
    END
END AS Weight,
-- Reps1
CASE
  WHEN Reps1 = NULL THEN 'N/A'
  ELSE
    CASE
      WHEN Type = 'Isolation' THEN
        CASE
          WHEN reps1 < '12'
               AND reps1 >= '8' THEN reps1 + ( 1 )
          ELSE '8'
        END
      ELSE '5'
    END
END AS Reps1,
-- Reps2
CASE
  WHEN Reps2 IS NULL THEN 'N/A'
  ELSE
    CASE
      WHEN Type = 'Isolation' THEN
        CASE
          WHEN reps2 < '12'
               AND reps2 >= '8' THEN reps2 + ( 1 )
          ELSE '8'
        END
      ELSE '5'
    END
END AS Reps2,
--Reps3
CASE
  WHEN Reps3 IS NULL THEN 'N/A'
  ELSE
    CASE
      WHEN Type = 'Isolation' THEN
        CASE
          WHEN reps3 < '12'
               AND reps3 >= '8' THEN reps3 + ( 1 )
          ELSE '8'
        END
      ELSE '5'
    END
END AS Reps3,
--Reps4
CASE
  WHEN Reps4 IS NULL THEN 'N/A'
  ELSE
    CASE
      WHEN Type = 'Isolation' THEN
        CASE
          WHEN reps4 < '12'
               AND reps4 >= '8' THEN reps4 + ( 1 )
          ELSE '8'
        END
      ELSE '5'
    END
END AS Reps4,
--Reps5
CASE
  WHEN Reps5 IS NULL THEN 'N/A'
  ELSE
    CASE
      WHEN Type = 'Isolation' THEN
        CASE
          WHEN reps5 < '12'
               AND reps5 >= '8' THEN reps5 + ( 1 )
          ELSE '8'
        END
      ELSE '5'
    END
END AS Reps5
FROM   workoutsessions
       JOIN exercisecultures
         ON workoutsessions.ExerciseID = ExerciseCultures.ExerciseID
       JOIN workoutdates
         ON workoutsessions.WorkoutID = Workoutdates.WorkoutID
            AND workoutdates.date = (SELECT Max(date)
                                     FROM   WorkoutDates
                                     WHERE  workouttype = 'Pull') 

1 个答案:

答案 0 :(得分:0)

在执行条件语句之前,尝试将整数类型转换为varchar类型。您可以通过创建变量/临时表,将这些列指定为VARCHAR - 然后从此临时表中提取数据来执行此操作,或者如果您不想这样做,则必须在查询中强制转换每个整数列,如此...

    case when CAST(Reps1 AS varchar(100)) = Null then 'N/A'
    else