select fname,lname,DATEDIFF(YY,birthdate,GETDATE()) as age from EMPLOYEE where age = 72
,但有一个错误说? - >“无效列名‘年龄’
我该如何解决呢谢谢
select fname,lname,DATEDIFF(YY,birthdate,GETDATE()) as age from EMPLOYEE where age = 72
,但有一个错误说? - >“无效列名‘年龄’
我该如何解决呢谢谢
在! SQL Server,则必须重复的表达(从select
)使用它where
子句中:除非使用一些
select fname,lname,DATEDIFF(Year,birthdate,GETDATE()) as age
from EMPLOYEE
where DATEDIFF(Year,birthdate,GETDATE()) = 72
像cross apply()
这样的东西来创建计算的字段。
谢谢!晚上好! –
@CelbisH。乐于帮助! – SqlZim
只是重复表达where
子句中:
select fname,
lname,
DATEDIFF(YY, birthdate, GETDATE()) as age
from EMPLOYEE
where DATEDIFF(YY, birthdate, GETDATE()) = 72
我最喜欢的方式在SQL Server为此采用了横向联接:
select e.fname, e.lname, v.age
from EMPLOYEE e cross apply
(values (DATEDIFF(year, birthdate, GETDATE())) v(age)
where v.age = 72;
注:
[不良习惯踢:使用速记与日期/时间操作 - 亚伦伯特兰](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/09/20/bad-habits-to-kick -using-short-date-time-operations.aspx) – SqlZim