我有一个表,上面有生日,我要选择未来30天内的所有生日。
情况是,所有的生日都以1999-09-15的格式书写,这意味着即使我尝试选择接下来的30天,也不会出现生日,因为那是1999年。>
我正在运行Microsoft Server 2016。
SELECT * from dbo.EMPLOYEES
WHERE DATE <= DATEADD(day, +30,GETDATE())
and DATE >= getdate()
order by "DATE"
答案 0 :(得分:3)
要获取出生日期,我们只需要在日期和月份上工作,而不需要在年份上工作。这就是为什么我们无法获得2个日期之间的Where日期的原因。
SELECT
dateofbirth_c AS BIRTHDAY
,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25) AS AGE_NOW
,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25) AS AGE_30_Days_FROM_NOW
FROM
Employees EMP
WHERE 1 = (FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25))
-
(FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25))
答案 1 :(得分:2)
尝试以下方法;检查月份和日期,因为年份与生日年份不匹配,这就是为什么您没有任何数据的原因。
SELECT *
from dbo.EMPLOYEES
WHERE month(DATE)>= month(GETDATE())
and day(DATE) >= day (getdate()) and day(DATE) < = day( getdate()) + 30
order by "DATE"
答案 2 :(得分:0)
尝试
SELECT *
FROM dbo.EMPLOYEES
WHERE DATEFROMPARTS(YEAR(GETDATE()) , MONTH(Date), DAY(Date)) >= GETDATE()
AND DATEFROMPARTS(YEAR(GETDATE()) , MONTH(Date), DAY(Date)) <= DATEADD(day, +30, GETDATE())
ORDER BY Date
答案 3 :(得分:0)
如果您希望获得适用于leap年等的准确结果,则:
private static DbManager buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context,
DbManager.class,
DB_NAME)
.addMigrations(MIGRATION_31_32, MIGRATION_33_35, MIGRATION_35_36, MIGRATION_36_37,MIGRATION_37_38)
.allowMainThreadQueries().build();
}
static final Migration MIGRATION_31_32 = new Migration(32, 33) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE 'Orders' " + " ADD COLUMN 'lon' REAL");
database.execSQL("ALTER TABLE 'Orders' " + " ADD COLUMN 'lat' REAL");
}
};
static final Migration MIGRATION_33_35 = new Migration(33, 35) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE 'Orders' " + " ADD COLUMN 'lats' REAL not null DEFAULT 0");
}
};
static final Migration MIGRATION_35_36 = new Migration(35, 36) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE 'change_job_status' " + " ADD COLUMN 'login' text");
}
};
static final Migration MIGRATION_36_37 = new Migration(36, 37) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE 'Orders' " + " ADD COLUMN 'category_nr' INTEGER not null DEFAULT 0");
}
};
static final Migration MIGRATION_37_38 = new Migration(37, 38) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE 'Orders' " + " ADD COLUMN 'status_date' text");
}
};