我试图更新员工薪酬,但如果他们与公司合作超过一年,我只想提高工资。我不知道怎么告诉查询一年的部分。我向所有员工加薪,而不仅仅是那些与公司合作超过一年的员工。我如何告诉SQL我只希望员工工作超过365天?
package com.entities;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@javax.persistence.Entity
@Table(name="users")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:4)
UPDATE
TEmployees
SET
monHourlyRate = monHourlyRate + 2
WHERE
dteHireDate <= DATEADD(year,-1,CAST(GETDATE() AS DATE))
您可能会看到一个常见的答案,例如下面的这个有效但如果闰年可能会关闭,因为闰年可能是366天。
UPDATE
TEmployees
SET
monHourlyRate = monHourlyRate + 2
WHERE
DATEDIFF(DAY,dteHireDate,CAST(GETDATE() AS DATE)) + 1 >= 365
答案 1 :(得分:2)
您可以使用
UPDATE
TEmployees
SET
monHourlyRate = monHourlyRate + 2
WHERE
dteHireDate < DATEADD(DAY, -365, CAST(GETDATE() AS DATE))
答案 2 :(得分:2)
UPDATE
TEmployees
SET
monHourlyRate = monHourlyRate + 2
WHERE
dteHireDate < DATEADD(YY,-1,CAST(GETDATE() AS DATE))
答案 3 :(得分:1)
试试这个:
UPDATE
TEmployees
SET
monHourlyRate = monHourlyRate + 2
WHERE
dteHireDate <= DATEADD(year,-1, (GETDATE())