我的数据库中有一个字段,用于保存员工的开始日期。
我想知道有多少员工在这个月有一周年纪念?
例如,如果员工开始在07/12/2012上工作,那么如果当前日期是01/12/2013,则是他/她的周年纪念日。
这是每个employee 07/12/2012的开始日期格式字符串。我只想比较月份和年份(而不是日期)。
有谁能告诉我怎么做吗?
发布于 2012-07-28 18:34:42
Transact-SQL中有MONTH和YEAR函数(假设您使用MSSQL)。(See here)
因此,如果您的表中有一个日期时间字段startdate
SELECT * FROM employee WHERE MONTH(startdate)=12 AND YEAR(startdate)=2012将在2012年12月为您的所有员工提供一周年纪念。
编辑
如果您的表包含字符串形式的日期,则可以使用SUBSTRING(startdate,3,2)获取月份(字符串形式!)和SUBSTRING(startdate,6,4),或者使用CONVERT或PARSE函数将其转换为日期时间(然后如上所述使用MONTH和YEAR )
第二次编辑
对于Linq to SQL,它将是
var result = from employees e
select e
where e.startdate.Month==DateTime.Now.Month
&& e.startdate.Year==DateTime.Now.Year;(仍然假设数据库中的startdate列具有DATETIME数据类型...)
发布于 2012-07-28 18:42:28
很简单:
SELECT *
From Member m
WHERE
MONTH(m.CreatedAt) = MONTH(GETDATE())如果你想要明年的周年纪念,你也可以使用:
SELECT *
From Member m
WHERE
MONTH(m.CreatedAt) = MONTH(GETDATE()) and
YEAR(m.CreatedAt) = YEAR(GETDATE()) +1发布于 2012-07-28 18:52:35
要在SQL中处理它,请尝试
SELECT * FROM [dbo].[Employee]
where (DATEPART(mm, [StartDate]) = Month(GetDate())
AND DATEPART(dd, [StartDate]) = Day(GetDate())在C#中
var aniveraries = from employee in employees
where
employee.StartDate.Month == DateTime.Now.Month &&
employee.StartDate.Year == DateTime.Now.Year
select employee;https://stackoverflow.com/questions/11700199
复制相似问题