SQL中兩個日期的查詢語句怎麼寫

2021-08-08 10:29:24 字數 2810 閱讀 4275

1樓:匿名使用者

1、建立測試表,

create table test_date(id int, v_date date);

2、插入測試資料

insert into test_date values (1, str_to_date('2016-01-02', '%y-%m-%d'));

insert into test_date values (2, str_to_date('2019-01-02', '%y-%m-%d'));

insert into test_date values (3, str_to_date('2019-11-02', '%y-%m-%d'));

insert into test_date values (4, str_to_date('2019-11-14', '%y-%m-%d'));

3、查詢原始表的記錄,select t.* from test_date t,

4、編寫sql,限定兩個日期,查詢記錄,

select * from test_date t where date_format(v_date, '%y-%m-%d') in ('2016-01-02','2019-11-02'),

2樓:匿名使用者

你是條件帶兩個日期,還是想提取兩個日期欄位?

select rq1,rq2 from table_name

where to_char(rq1, 'yyyy-mm-dd') between '2012-04-01' and '2012-04-01' ;

3樓:匿名使用者

這個要區分資料庫的。如果是informix、mysql、sybase可以直接以字串形式查詢,如果是oracle那就需要加to_date函式。

不用寫什麼between and ,直接寫大於小於號即可。

寫字串的時候注意標準格式:2012-04-20 10:00:00 或者2012/04/20 10:00:00

4樓:匿名使用者

查詢當前系統時間為select sysdate from dual;

查詢兩個日期之間的月數(date1-date2)用months_between(date1,date2)單值函式。例如:

select months_between(hiredate,sysdate) from emp;

sql語句查詢特定時間段的資料怎麼寫

5樓:海天盛筵

sql伺服器:

select*fromtablewhere'2008-7-1'和'2008-12-31'

訪問:62616964757a686964616fe78988e69d8331333433626537

從表中選擇發生日期》#2008-7-1#和發生日期<#2008-12-31#

就是這樣:注意,sqlserver中的日期和訪問有一點不同。

擴充套件資料:

sql查詢日期語句

select*fromshoporderwheredatediff(week,ordtime,getdate()-1)=0//查詢第一年的日期

select*fromshoporder,其中datediff(day,ordtime,getdate()-1)=0//查詢當天的所有資料

select * from a where datediff(d,datetime,getdate()) <=30 //前30天

select * from a where datediff(m, shijian, getdate()) <=1 //上個月

搜尋當天記錄的其他方法:

select*

fromj_gradeshop

其中(gaddtimebetweenconvert(datetime,left(getdate(),10)+'00:00:00.000'))

並轉換(datetime,left(getdate(),10)+'00:00:00.00.000')+1)

由gaddtime指定的訂單

6樓:匿名使用者

select * from 表 where 日期du字zhi段dao

內>='開始日期' and 日期欄位

<='截止日期'

and convert(char(8),日期欄位,108)>='開始時間' and convert(char(8),日期欄位,108)<='截止時間'

例如容:

select * from tb1 where ddate>='2010-11-05' and ddate<='2010-11-15'

and convert(char(8),ddate,108)>='22:30:00' and convert(char(8),ddate,108)<='23:00:00'

7樓:匿名使用者

select * from table_name where convert(char(14),你的

bai時du間字zhi段dao名版稱權,20) between '20100701192000' and '20100731212000'

8樓:

是抄sql server資料庫吧襲

。表table1,欄位d,如下

select * from table1

where year(d)=2010 and month(d)=7 and day(d) between 1 and 31

and (datepart(hour,d)>=22 or datepart(hour,d)<6)

sql語句先要查詢滿足兩個條件的查詢語句,然後統計滿足兩個條

這樣試試 select count from jtwh zb guzhang where quick y and gzok handling sql查詢兩個表中滿足某些條件的資料總數 如果欄位一樣的話,可以用union all,即select from 表1 where a,b,cunion all...

sql兩個表多列聯合查詢,sql語句 同時查詢兩個表

select a.b.備註 from select 姓名,一班,一班 as 班級 from 課程 表 union all select 姓名,二班,二班 as 班級 from 課程 表 union all select 姓名,三班,三班 as 班級 from 課程 表 a left join 流水錶...

兩個SQL語句的差值

select value,max time min time from d live data where field id 156 group by value 這樣嗎bai du,你之前那zhi 個不daogroup by能執行 回成功嗎?答 在sql語句中怎樣計算出兩個日期的差值 在sql語句...