sqldatetime查詢
1. sql中datetime查詢出現問題是怎麼處理
SQL 中使用 convert函數來格式化datetime的顯示格式。
語法
CONVERT(data_type(length),data_to_be_converted,style)
data_type(length)規定目標數據類型(帶有可選的長度)。data_to_be_converted含有需要轉換的值。style規定日期/時間的輸出格式。
可以使用的style值:
2. sqlserver的datetime查詢
sql的datetime查詢使用
convert函數格式化輸出格式
convert函數語法:
CONVERT(data_type(length),data_to_be_converted,style)
data_type(length)規定目標數據類型(帶有可選的長度)。data_to_be_converted含有需要轉換的值。style規定日期/時間的輸出格式。
可以使用的style值如圖:
例:
3. sql資料庫里datetime類型怎麼查詢
用CONVERT()
函數阿。
函數語法:CONVERT(data_type(length),data_to_be_converted,style)
假設你輸入的key值是yymmdd這樣的格式,那麼你可以這樣寫:
select
*
from
news
where
convert(VARCHAR(100),infotime,112)
like
'%"&
key
&"%'
order
by
id
desc
如果輸入的key是其它格式,那麼style要改,網上有對應表,你可以去查
4. 如何從sql server資料庫中查詢datetime類型的數據詳細些!!!
這是查詢與當前時間在同一周內的數據,sql語句是:
select * from 表 where datediff(week,時間欄位,getdate())=0
也可以稍作修改用來查詢與指定日期在同一周內的數據:
select * from 表 where datediff(week,時間欄位,'2002-01-01')=0
像你的這個表,查詢一段時間(需要指定前後日期的)
select * from 表 where startdate between 指定日期 and 指定日期
select * from 表 where leavedate between 指定日期 and 指定日期
查詢一段時間( 不需要固定時間的,只需要前後間隔時間的)
select * from 表 where datediff(day,startdate,leavedate)<7 and startdate = 指定日期select * from 表 where datediff(day,startdate,leavedate)<7 and leavedate = 指定日期
這是查詢此日期開始時間到離開時間在七天之間的所有數據,後面的and部分按需要加。
5. sql怎麼查詢datetime
SELECT COUNT(*)/datediff(hour,'2009-06-15 00:00:00','2009-06-28 00:00:00')
from a
where b >= '2009-06-15 00:00:00'and b <= '2009-06-28 00:00:00'
如上是查詢6月15日到28日期間,平均每小時記錄的數據數目
datediff(hour,'2009-06-15 00:00:00','2009-06-28 00:00:00' --取得日期區間的小時數
------------------------------------------
哦,是我讀題不仔細唉,呵呵。
這樣的話就復雜了。
你不急的話我明天有空寫個預存程序來解決這個問題
------------------------------------------
1.先建立日期時間構造預存程序,用來傳回作時間段查詢的兩個時間
create PROC [dbo].[sp_fordate]
@year char(4),
@month char(2),
@day char(2),
@hour char(2),
@date1 varchar(12) output,
@date2 char(20) output,
@date3 char(20) output
as
begin
set @date1 = @year + '-' + ltrim(rtrim(@month)) + '-' +rtrim(@day)
set @date2 = @date1 + space(1) + ltrim(rtrim(@hour)) + ':00:00'
set @hour = @hour +1
set @date3 = @date1 + space(1) + ltrim(rtrim(@hour)) + ':00:00'
set @hour = @hour -1
set @date1 = @date1 + '-'+ @hour
end
2.在這一個預存程序里用循環跑出一個用union關聯的超長sql語句執行。需要傳入四個值,年份(2009四位格式),月份,日期(若日期傳入為空自動取1),小時(輸入1表示1點到2點時段,以此類推)
create proc riqi
@year char(4),
@month char(2),
@day char(2),
@hour char(2)
as
BEGIN
declare @SQL varChar(4000)
if @day is null or @day = ''
begin
set @day = 1
end
declare @date1 char(12)
declare @date2 char(20)
declare @date3 char(20)
exec sp_fordate @year,@month,@day,@hour,@date1 output,@date2 output,@date3 output
set @sql = 'select count(*) as 數目, '''+ @date1 + ''' as 日期時間 from a where b >= ''' + @date2 + '''' + ' and b<= ''' + @date3 + ''''
set @day = @day + 1
while @day <= 31
BEGIN
exec sp_fordate @year,@month,@day,@hour,@date1 output,@date2 output,@date3 output
set @sql = @sql +char(10)+char(13)+ ' union all ' +char(10)+char(13)+ 'select count(*), '''+ @date1 + ''' from a where b >= ''' + @date2 + '''' + ' and b<= ''' + @date3 + ''''
set @day = @day + 1
print @day
if @@error <> ''
BEGIN
break
END
ELSE
CONTINUE
END
exec(@sql)
END
測試結果如下
數目 日期時間
----------- ------------
3 2009-10-1-1
2 2009-10-2-1
1 2009-10-3-1
最後的-1表示時間段是一點
唉,只想到這個笨辦法唉
6. 如何查詢sql資料庫中的datetime的某一年的記錄
select
sum(收入),sum(支出)
from
表名
where
year(日期)=2013
year函數就是獲取datetime的年份
7. sql server怎麼按時間查詢datetime
sql server 中,
可以用cast()函數將日期時間轉換為日期,
比如:cast('2014-01-22 13:22:35.000' as date) 的結果為2014-01-22
以下語句是查詢2012年的數據,日期范圍可以修改
select * 表名
where CAST(時間欄位 as date) between '2012-01-01' and '2012-12-31'
如果要查全年數據,也可以這樣,
select * 表名 where year(時間欄位)=2012
另外,用convert()函數也可以將日期時間欄位轉換為日期欄位來代替cast,具體用法一下
如果是oracle資料庫請用to_date()代替cast將日期時間欄位轉換為日期來查詢
8. sql server中datetime類型如何只輸入日期將信息查詢出來
嘗試下模糊匹配或者用日期區間查詢
1.select * from yourtable
where FSaveDate like '2012-12-06%'
2.selet * from yourtable
where FSaveDate between '2012-12-06' and '2012-12-07'
9. SQL 如何查詢日期在一定范圍內的數據
select * from 表 where 日期欄位>='開始日期' and 日期欄位<='截止日期' and convert(char(8),日期欄位,108)>='開始時間' and convert(char(8),日期欄位,108)<='截止時間'。
SELECT * FROM 表明 WHERE 日期欄位名 BETWEEN '20130101' AND '20130130'。
例如:
select * from tb1 where dDate>='2010-11-05' and dDate<='2010-11-15'
and convert(char(8),dDate,108)>='8:00:00' and convert(char(8),dDate,108)<='9:00:00'.
select * from table1where year(d)=2010 and month(d)=7 and day(d) between 1 and 31
and (Datepart(hour,d)>=22 or Datepart(hour,d)<6)
(9)sqldatetime查詢擴展閱讀:
SQL查詢日期:
今天的所有數據:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())=0
昨天的所有數據:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())=1
7天內的所有數據:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())<=7
30天內的所有數據:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())<=30
本月的所有數據:select * from 表名 where DateDiff(mm,datetime類型欄位,getdate())=0
本年的所有數據:select * from 表名 where DateDiff(yy,datetime類型欄位,getdate())=0
參考資料:SQL_網路
10. 在sql server中,記錄日期的欄位是datetime類型,請問怎麼查詢一天的數據
select * from info where dateTime>'2001-12-01 00:00:00' and dateTime<'2001-12-01 23:59:59'
這樣寫錯不了,昨天我還在用,DateTime為欄位名
當然,你報錯的原因很可能是將最大時間寫成59的緣故,最到23點而不是59點