資料庫取前N天資料

2021-08-08 11:35:03 字數 1389 閱讀 9706

oracle資料庫中:

首先要區分,時間欄位是日期格式,還是字串

日期格式:

trunc(sysdate - 1);

字串格式:to_char(sysdate - 30,' yyyy-mm-dd ')

選前1天資料:

select * from 表

where 日期》=to_date(to_char(sysdate-1,'yyyy/mm/dd'),'yyyy/mm/dd');

或:
select * from 表

where 日期》=trunc(sysdate - 1);

選前7天資料:

select * from 表

where 日期=to_date(to_char(sysdate-7,'yyyy/mm/dd'),'yyyy/mm/dd');

或:
select * from 表

where 日期》=trunc(sysdate - 7);

選前30天資料:

select * from 表

where 日期=to_date(to_char(sysdate-30,'yyyy/mm/dd'),'yyyy/mm/dd');

或:
select * from 表

where 日期》=trunc(sysdate - 30);

選前一年資料:

select * from 表

where 日期=to_date(to_char(add_months(sysdate,-12),'yyyy/mm/dd'),'yyyy/mm/dd');

ole db源  取30天資料(sqlserver資料庫):

select * 

from t_jcxx

where f_sdrq >= convert(varchar(100), dateadd(day,-30,getdate()), 23)

取前2月資料:
select * 

from t_jcxx

where f_sdrq >= convert(varchar(100), dateadd(

month

,-2,getdate()), 23)

取前1年資料:
select * 

from t_jcxx

where f_sdrq >= convert(varchar(100), dateadd(

year

,-1,getdate()), 23)

取資料庫表前N條記錄,對於的SQL

取一表前n條記錄,各個資料庫的sql寫法 1.oracle select from table1 where rownum n 2.db2 select row number over order by col1 desc as rownum where rownum n db2 select co...

Oracle 取前 n 行資料詳解

2 擴充套件 select t.from table name t where rownum n select tt.from select t.from table name t order by t.column name tt where rownum n with student info ...

sql查詢指定日期前n天資料

查詢2019 08 28前7天的記錄,包括2019 08 28 select from a where timestampdiff day,a.create time,2019 08 28 7 and timestampdiff day,a.create time,2019 08 28 0擴充套件 ...