我正在尝试这样做
SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
FROM batch
WHERE (NewDT <= @BatchStartDate)
但我收到错误“无效的列名 NewDT”问题是日期和时间在数据库的两个列中。
我正在尝试这样做
SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
FROM batch
WHERE (NewDT <= @BatchStartDate)
但我收到错误“无效的列名 NewDT”问题是日期和时间在数据库的两个列中。
猜测您正在使用 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)
(也猜你用的是SQL Server)
您不能在 where 子句中使用别名,因此您可以使用整个表达式(如 Yaroslav 的答案),或者您可以将原始选择包装为子查询,如下所示:
select * from
(
SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
FROM batch
)
WHERE NewDT <= @BatchStartDate