来自@Parameter的价值

时间:2012-10-17 06:53:05

标签: sql parameters casting

我试图这样做

SELECT     CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
FROM         batch
WHERE     (NewDT <= @BatchStartDate)

但我收到错误&#34;无效的列名NewDT&#34; 问题是日期和时间在数据库中有两个列。

2 个答案:

答案 0 :(得分:2)

猜测您正在使用MS SQL Server。你不能在WHERE子句中引用别名,你应该使用完整的CAST(date AS datetime) + CAST(time AS datetime)所以它会是:

SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
  FROM batch
 WHERE (CAST(date AS datetime) + CAST(time AS datetime) <= @BatchStartDate)

答案 1 :(得分:0)

(还猜测你使用的是SQL Server)

你不能在where子句中使用别名,所以你可以使用整个表达式(如Yaroslav的答案),或者你可以将原始select包装为子查询,如下所示:

select * from 
(
   SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
   FROM batch
)
WHERE NewDT <= @BatchStartDate