简体   繁体   English

sql从列计算日期

[英]sql calculate date from column

Hi I need a bit of advice with the following question which reads as follows: Provide the sql that will determine the length of employment for each employee. 嗨,我需要以下问题的一些建议,内容如下:提供确定每个雇员的工作年限的sql。 For each employee display lastname and calulate number of years between today and the date on which the employee was hired. 为每位员工显示姓氏,并计算从今天到雇用该员工的日期之间的年数。 Label the column 'YEARS' 将列标记为“ YEARS”

在此处输入图片说明

Im not sure how to approach this, should I use the datediff() function? 我不确定如何处理此问题,应该使用datediff()函数吗? Can I do something like this: 我可以做这样的事情:

SELECT DATEDIFF(HIRE_DATE,'2014-09-29') AS DiffDate 

Also how do I format DiffDate then that the output will only be years? 另外,如何格式化DiffDate,然后输出将只有几年?

Thanks 谢谢

use as 用于

SELECT LastName, DATEDIFF('day',HIRE_DATE,'2014-09-29') AS DiffDate from TableName

I am assuming that Table contains LastName column. 我假设Table包含LastName列。

Reference 参考

The correct syntax for DATEDIFF could be found here http://msdn.microsoft.com/en-us/library/ms189794.aspx 有关DATEDIFF的正确语法,请参见http://msdn.microsoft.com/zh-cn/library/ms189794.aspx

SELECT DATEDIFF(day, HIRE_DATE,'2014-09-29') AS DiffDate 
From yourtable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM