當前位置:首頁 » 編程語言 » sql查詢當天數據

sql查詢當天數據

發布時間: 2024-03-28 14:45:19

1. sql語句如何統計資料庫當日信息數量

你說的當日是指某一天嗎?首先你的時間欄位的類型必須是datetime類型,如果不是就轉換一下。

查詢語句可以這樣寫,假設我們要查詢2006年6月1日這一天的數據量。我不知道你的時間欄位是不是datetime類型,所以進行了轉換。

select count(*)
from 表
where convert(varchar(8),cast(時間欄位 as datetime),112)='20060601'

把「表」和「時間欄位」替換成實際的名稱就可以了

2. SQL查某一天的數據怎麼查

你這個是SQL server,查詢方式不一樣
select * from data where (括弧裡面是列名稱) between '2012-11-11' and '2012-11-12'

3. 如何用SQL語句,查詢資料庫中等於某一天的記錄

select*from表名where日期欄位='2017-01-01'
--如果是帶有時間的話
select*from表名where日期欄位>='2017-01-0100:00:00'and日期欄位<='2017-01-0123:59:59'
--或者簡單一點
select*from表名where日期欄位>='2017-01-01'and日期欄位<'2017-01-02'

4. sql查詢當天記錄

1、SQL在查詢當天記錄時要注意是從當天的0點0分0秒0毫秒開始,到次日0點0分0秒0毫秒截止,但不包含次日的0點0分0秒0毫秒。

2、注意:在不同資料庫產品中,獲得當天日期的函數不一樣。

MSSQL獲得當前日期:convert(varchar(10),Getdate(),120)

MYSQL獲得當前日期:date(now())

Oracle獲得當前日期:to_char(sysdate,'yyyy-mm-dd')

Access獲得當前日期:date()

3、在各個資料庫里獲得當天的記錄寫法為(假設表名為:Table_1,日期列名為:date_col):

MSSQL獲得當天記錄:

select*fromtable_1wheredate_col>=convert(varchar(10),Getdate(),120)anddate_col<convert(varchar(10),dateadd(d,1,Getdate()),120)

MYSQL獲得當天記錄:

select*fromtable_1wheredate_col>=date(now())anddate_col<DATE_ADD(date(now()),INTERVAL1DAY)

Oracle獲得當天記錄:

select*fromtable_1wheredate_col>=to_char(sysdate,'yyyy-mm-dd')anddate_col<to_char(sysdate+1,'yyyy-mm-dd')

Access獲得當天記錄:

select*fromtable_1wheredate_col>=date()anddate_col<DateAdd("d",1,date())

4、另外,在查詢的時候,盡量不要對列進行運算,因為日期列上若有索引,就無法使用索引了。

5. 如何在SQL中按時間段查詢數據

sql server:

select * from 表 where 發生日期>'2008-7-1' and 發生日期<'2008-12-31'

access:

select * from 表 where 發生日期>#2008-7-1# and 發生日期<#2008-12-31#

這樣就可以了,注意sql server與access中的日期有一點不一樣。

(5)sql查詢當天數據擴展閱讀:

sql查詢日期語句

select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查詢當天日期在一周年的數手燃宏據

select * from ShopOrder where 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 *

FROM j_GradeShop

WHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')

AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1)

ORDER BY GAddTime DESC

6. 使用sql語句查詢日期的方法

使用sql語句查詢日期的方法

結構化查詢語言(Structured Query Language)簡稱SQL,是一種特殊目的的編程語言,是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統。以下是我為大家整理的使用sql語句查詢日期的方法,僅供參考,希望能夠幫助大家。

使用sql語句查詢日期的方法 篇1

使用sql語句查詢日期

select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查詢當天日期在一周年的數據

--查詢當天:

select * from info where DateDiff(dd,datetime,getdate())=0

--查詢24小時內的:

select * from info where DateDiff(hh,datetime,getDate())<=24

--info為表名,datetime為資料庫中的欄位值

--查詢當天:

select * from info where DateDiff(dd,datetime,getdate())=0

--查詢24小時內的:

select * from info where DateDiff(hh,datetime,getDate())<=24 --info為表名,datetime為資料庫中的欄位值

Sql代碼

--查詢當天記錄另類的方法

SELECT *

FROM j_GradeShop

WHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')

AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1) ORDER BY GAddTime DESC

--查詢當天記錄另類的方法

SELECT *

FROM j_GradeShop

WHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')

AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1) ORDER BY GAddTime DESC

DATEDIFF 函數:

語法:

DATEDIFF ( datepart , startdate , enddate )

備注:enddate 減去 startdate。如果 startdate 晚於 enddate,則返回負值。 如果結果超出整數值范圍,則 DATEDIFF 將產生錯誤。對於毫秒,最大數是 24 天 20 小時 31 分鍾零 23.647 秒。對於秒,最大數是 68 年。

跨分鍾、秒和毫秒等邊界計算的方法使得 DATEDIFF 指定的結果在所有數據類型中均一致。結果是帶正負號的整數值,它等於跨第一個和第二個日期間的

datepart 邊界數。例如,在 1 月 4 日(星期日)和 1 月 11 日(星期日)之間的'星期數是 1。

可以再MSSQL中測試:

Sql代碼

--兩個時間差剛好是24

--列印的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

--查詢的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

--兩個時間差剛好是24

--列印的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

--查詢的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

Sql代碼

--本月記錄

SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0

--本周記錄

SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0

--包括本年這些查詢方式是一樣的

--本月記錄

SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0

--本周記錄

SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0

--包括本年這些查詢方式是一樣的

sql server中的時間函數

1. 當前系統日期、時間

select getdate()

2. dateadd 在向指定日期加上一段時間的基礎上,返回新的 datetime 值 例如:向日期加上2天

select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000

3. datediff 返回跨兩個指定日期的日期和時間邊界數。

select datediff(day,'2004-09-01','2004-09-18') --返回:17

4. datepart 返回代表指定日期的指定日期部分的整數。

SELECT DATEPART(month, '2004-10-15') --返回 10

5. datename 返回代表指定日期的指定日期部分的字元串

SELECT datename(weekday, '2004-10-15') --返回:星期五

6. day(), month(),year() --可以與datepart對照一下

select 當前日期=convert(var10),getdate(),120)

,當前時間=convert(var8),getdate(),114)

select datename(dw,'2004-10-15')

select 本年第多少周=datename(week,'2004-10-15')

,今天是周幾=datename(weekday,'2004-10-15')

函數 參數/功能

GetDate( ) 返回系統目前的日期與時間

DateDiff (interval,date1,date2) 以interval 指定的方式,返回date2 與date1兩個日期之間的差值 date2-date1

DateAdd (interval,number,date) 以interval指定的方式,加上number之後

的日期

DatePart (interval,date) 返回日期date中,interval指定部分所對應的整數值

DateName (interval,date) 返回日期date中,interval指定部分所對應的字元串名稱

參數 interval的設定值如下:

值 縮 寫(Sql Server) Access 和 ASP 說明

Year Yy yyyy 年 1753 ~ 9999

Quarter Qq q 季 1 ~ 4

Month Mm m 月1 ~ 12

Day of year Dy y 一年的日數,一年中的第幾日 1-366

Day Dd d 日,1-31

Weekday Dw w 一周的日數,一周中的第幾日 1-7

Week Wk ww 周,一年中的第幾周 0 ~ 51

Hour Hh h 時0 ~ 23

Minute Mi n 分鍾0 ~ 59

Second Ss s 秒 0 ~ 59

Millisecond Ms - 毫秒 0 ~ 999

access 和 asp 中用date()和now()取得系統日期時間;其中

DateDiff,DateAdd,DatePart也同是能用於Access和asp中,這些函數的用法也類似

舉例:

1.GetDate() 用於sql server :select GetDate()

2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值為 514592 秒 DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值為 5 天

3.DatePart('w','2005-7-25 22:56:32')返回值為 2 即星期一(周日為1,周六為7)

DatePart('d','2005-7-25 22:56:32')返回值為 25即25號

DatePart('y','2005-7-25 22:56:32')返回值為 206即這一年中第206天 DatePart('yyyy','2005-7-25 22:56:32')返回值為 2005即2005年

使用sql語句查詢日期的方法 篇2

datediff(week,zy_time,getdate())=0 //查詢本周 datediff(month,zy_time,getdate())=0 //查詢本月

本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0

前半年1-6,後半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7 1. 當前系統日期、時間 select getdate()

2. dateadd 在向指定日期加上一段時間的基礎上,返回新的 datetime 值 例如:向日期加上2天

select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000 3. datediff 返回跨兩個指定日期的日期和時間邊界數。

select datediff(day,'2004-09-01','2004-09-18') --返回:17 4. datepart 返回代表指定日期的指定日期部分的整數。 SELECT DATEPART(month, '2004-10-15') --返回 10 5. datename 返回代表指定日期的指定日期部分的字元串 SELECT datename(weekday, '2004-10-15') --返回:星期五 6. day(), month(),year() --可以與datepart對照一下 select 當前日期=convert(var10),getdate(),120) ,當前時間=convert(var8),getdate(),114) select datename(dw,'2004-10-15')

select 本年第多少周=datename(week,'2004-10-15') ,今天是周幾=datename(weekday,'2004-10-15') 函數 GetDate( )

參數/功能

返回系統目前的日期與時間

以interval 指定的方式,返回

DateDiff

date2 與date1兩個日期之間的

(interval,date1,date2)

差值 date2-date1 DateAdd 以interval指定的方式,加上(interval,number,date) number之後的日期

返回日期date中,interval指定

DatePart (interval,date)

部分所對應的整數值 返回日期date中,interval指定

DateName (interval,date)

部分所對應的字元串名稱

參數 interval的設定值如下:

Access 和

縮 寫(Sql Server)

ASP Yy Qq Mm

yyyy q m y d w ww h n s -

值 Year Quarter Month

說明

年 1753 ~ 9999 季 1 ~ 4 月1 ~ 12

一年的日數,一年中的第幾日 1-366 日,1-31

一周的日數,一周中的第幾日 1-7

周,一年中的第幾周 0 ~ 51 時0 ~ 23 分鍾0 ~ 59 秒 0 ~ 59 毫秒 0 ~ 999

Day of year Dy Day Weekday Week Hour Minute Second

Dd Dw Wk Hh Mi Ss

Millisecond Ms

access 和 asp 中用date()和now()取得系統日期時間;其中DateDiff,DateAdd,DatePart也同是能用於Access和asp中,這些函數的用法也類似

舉例:

1.GetDate() 用於sql server :select GetDate()

2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值為 514592 秒 DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值為 5 天

3.DatePart('w','2005-7-25 22:56:32')返回值為 2 即星期一(周日為1,周六為7) DatePart('d','2005-7-25 22:56:32')返回值為 25即25號

DatePart('y','2005-7-25 22:56:32')返回值為 206即這一年中第206天 DatePart('yyyy','2005-7-25 22:56:32')返回值為 2005即2005年 sql 查詢本周本月問題 ---求相差天數

select datediff(day,'2004-01-01',getdate())

--1.一個月第一天的

SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

--2.本周的星期一

SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)

select dateadd(wk,datediff(wk,0,getdate()),6) --3.一年的第一天

SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

--4.季度的第一天

SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)

--5.當天的半夜

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

--6.上個月的最後一天

SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

--7.去年的最後一天

SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))

--8.本月的最後一天

SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))

--9.本年的最後一天

SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))

--10.本月的第一個星期一 select DATEADD(wk,

DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0) --查詢本周注冊人數

select count(*) from [user]

where datediff(week,create_day-1,getdate())=0

--上周注冊人數

select count(*) from [user]

where datediff(week,create_day-1,getdate())=1

--本月注冊人數

select count(*) from [user]

where datediff(month,create_day,getdate())=0

--上月注冊人數

select count(*) from [user]

where datediff(month,create_day,getdate())=1

--如果要效率,這樣寫查詢

--查詢本周注冊人數

where

create_day>=dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112)) and

create_day<dateadd(day,9-datepart(weekday,getdate()),convert(varchar,getdate(),112))

--上周注冊人數

select count(*) from [user] where

create_day>=dateadd(day,-5-datepart(weekday,getdate()),convert(varchar,getdate(),112)) and

create_day<dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112))

--本月注冊人數

select count(*) from [user]

where create_day>=dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)) and

create_day<dateadd(month,1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))

--上月注冊人數

select count(*) from [user] where

create_day>=dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))

and create_day<dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

--本周

select count(*) from User

where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())

--上周

select count(*) from User

where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7

--本月

select count(*) from User

where datepart(mm,create_day) = datepart(mm,getdate()) --上月

where datepart(mm,create_day) = datepart(mm,getdate()) - 1 --本周

select count(*) from [User]

where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())

--上周

select count(*) from [User]

where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7

--本月

select count(*) from [User]

where datepart(mm,create_day) = datepart(mm,getdate()) --上月

select count(*) from [User]

where datepart(mm,create_day) = datepart(mm,getdate()) - 1 學習

month(create_day)=month(getdate())本月 month(create_day)=month(getdate())-1 上月

補充 查詢今日所有的

SELECT * from feedback WHERE (DATEDIFF(d,fedtime,GETDATE())=0) ORDER BY fedid DESC

;

7. 查詢SQL中日期為當天的記錄

SELECT * FROM Table WHERE time= DATE_FORMAT(NOW(),'%Y-%m-%d')
使用date_format主要是為了格式化時間,使兩邊的時間格式一樣,我建議還是格式化一下比較好,防止出錯

熱點內容
pythontkinter大小 發布:2024-04-27 14:51:22 瀏覽:114
pc端好用的c語言編譯器 發布:2024-04-27 14:50:22 瀏覽:502
爬蟲腳本如何運行在伺服器 發布:2024-04-27 14:50:22 瀏覽:1
dropzone上傳 發布:2024-04-27 14:39:31 瀏覽:880
ins安卓版快拍為什麼沒有特效 發布:2024-04-27 14:33:41 瀏覽:592
cs伺服器ip在哪裡 發布:2024-04-27 14:25:58 瀏覽:37
華為安卓怎麼上臉書 發布:2024-04-27 14:24:20 瀏覽:841
我的世界手機版伺服器冷知識 發布:2024-04-27 14:11:10 瀏覽:790
文件橫向加密 發布:2024-04-27 14:06:38 瀏覽:497
python列表推導 發布:2024-04-27 14:01:46 瀏覽:357