SQL SQL查询以检查今天是否是员工的生日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8803906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Query to check if today is Employee's birthday
提问by Vishal Avhad
Can any one tell me how to get the list of an employee who have their birthdays today ....
谁能告诉我如何获得今天过生日的员工的名单....
Thanks, Vishal
谢谢,维沙尔
回答by Miika L.
SELECT *
FROM Employees
WHERE DATEPART(d, DateOfBirth) = DATEPART(d, GETDATE())
AND DATEPART(m, DateOfBirth) = DATEPART(m, GETDATE())
回答by Andy Pollitt
Ideally a bit more information on the structure of your data sources would help, but a simple answer providing your employee records have a D.O.B. field would be to compare the day and month of this against the current system date in the where clause of your query.
理想情况下,有关数据源结构的更多信息会有所帮助,但如果您的员工记录具有 DOB 字段,一个简单的答案就是将其中的日期和月份与查询的 where 子句中的当前系统日期进行比较。
Something along the following lines:
大致如下:
select * from wherever
where
....
(
datepart(d, EmployeeDOB) = datepart(d, getdate()) and
datepart(m, EmployeeDOB) = datepart(m, getdate())
)
.....
回答by MatBailie
Although this is much more long winded, it does avoid having to scan the entire table looking for matches.
尽管这要冗长得多,但它确实避免了必须扫描整个表寻找匹配项。
WITH
a_century AS
(
SELECT 1 AS year
UNION ALL
SELECT year * 2 + 0 AS year FROM a_century WHERE year < 64
UNION ALL
SELECT year * 2 + 1 AS year FROM a_century WHERE year < 64
)
SELECT
*
FROM
yourTable
INNER JOIN
a_century
ON yourTable.birthday = DATEADD(year, -a_century.year, DATEADD(day, DATEDIFF(day, 0, getDate()), 0))
回答by Michael Krelin - hacker
WHERE
month and day of employee's birthday match today's month and day (obviously). More specific instructions would require more input.
WHERE
员工生日的月份和日期匹配今天的月份和日期(显然)。更具体的指令需要更多的输入。
回答by aF.
You can do something like this:
你可以这样做:
select e.name from employeeTable e
where right(e.birthday) = right(convert(varchar(8), getdate(), 112), 4)
or
或者
select e.name from employeeTable e
where datepart(d, e.birthday) = datepart(d, getdate())
and datepart(m, e.birthday) = datepart(m, getdate())