本文介绍了我想计算从员工加入之日起的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有临时表,其中包含以下数据
Id UserId UserFirstName DOJ位置
1 5 Ashvini 2016-05-03 00:00:00.000孟买
2 18 Deepak 2016-06-13 00:00:00.000 Mumbai
3 23 Devesh 2016-05-02 00:00:00.000 Mumbai
4 25 Dibyajyoti Arabinda 2016-07-06 00:00 :00.000 Mumbai
5 27 Gaurav 2016-08-16 00:00:00.000 Mumbai
6 38 Izhar 2016-05-23 00:00:00.000 Mumbai
7 39 Jacob 2016-04- 21 00:00:00.000 Mumbai
8 47 Mamun Al 2016-08-04 00:00:00.000 Mumbai
9 49 Manoj 2016-08-22 00:00:00.000 Mumbai
10 54 Nagesh 2016-06-01 00:00:00.000 Mumbai
11 55 Namit 2016-07-04 00:00:00.000 Mumbai
现在我想计算从加入日期到今天的总月份数
我尝试过的事情:
声明@count int,@ Count1 int,@ DOJ datetime,@ COuntdate int,@ Id int
set @count =(从#MyTemp1中选择计数(UserID))
设置@ Count1 = 1
while(@ Count1& lt; = @ count)
begin
set @ Id =(从#MyTemp1中选择Id,其中Id = @ Count1)
set @DOJ =(从#MyTemp1中选择DOJ,其中@ Id = @ Count1)
set @ COuntdate =(选择DATEDIFF(月,(从#MyTemp1选择DOJ,其中Id = @ Count1),GETDATE())为Vintage)
- set @ COuntdate =(选择DATEDIFF(月,@ DOJ,getdate()))
--CASE
- 当DATEPART(白天,@ DOJ)> DATEPART(day,getdate())THEN 1 ELSE 0
--END
print @COuntdate
END
set @ Count1 = @ Count1 + 1
但它给出错误
解决方案
I have temp table which has following data
Id UserId UserFirstName DOJ Location 1 5 Ashvini 2016-05-03 00:00:00.000 Mumbai 2 18 Deepak 2016-06-13 00:00:00.000 Mumbai 3 23 Devesh 2016-05-02 00:00:00.000 Mumbai 4 25 Dibyajyoti Arabinda 2016-07-06 00:00:00.000 Mumbai 5 27 Gaurav 2016-08-16 00:00:00.000 Mumbai 6 38 Izhar 2016-05-23 00:00:00.000 Mumbai 7 39 Jacob 2016-04-21 00:00:00.000 Mumbai 8 47 Mamun Al 2016-08-04 00:00:00.000 Mumbai 9 49 Manoj 2016-08-22 00:00:00.000 Mumbai 10 54 Nagesh 2016-06-01 00:00:00.000 Mumbai 11 55 Namit 2016-07-04 00:00:00.000 Mumbai
Now I want to calculate total number of month from Date of joining to today date
What I have tried:
Declare @count int,@Count1 int, @DOJ datetime,@COuntdate int,@Id int set @count =(select count(UserID) from #MyTemp1) set @Count1=1 while(@Count1<=@count) begin set @Id=(select Id from #MyTemp1 where Id=@Count1) set @DOJ = (select DOJ from #MyTemp1 where @Id=@Count1) set @COuntdate= (select DATEDIFF(MONTH, (select DOJ from #MyTemp1 where Id=@Count1),GETDATE()) as Vintage) --set @COuntdate= (select DATEDIFF (month, @DOJ, getdate())) --CASE -- WHEN DATEPART(day, @DOJ) > DATEPART(day, getdate()) THEN 1 ELSE 0 --END print @COuntdate END set @Count1=@Count1+1
but it gives error
解决方案
这篇关于我想计算从员工加入之日起的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!