开发者

How do I write a SQL query for a specific date range and date time using SQL Server 2008?

I need to write a query that will get all the rows in a specified date range and time range.

For example, I want to query all the rows from 5:00PM on 16-Sep-2010 to 9:00AM on 21-Sep-2010.

Any ideas as to how the query 开发者_运维百科should be?


SELECT * FROM TABLE
WHERE DATE BETWEEN '09/16/2010 05:00:00' and '09/21/2010 09:00:00'


Infact this worked for me

 SELECT * 
 FROM myTable
 WHERE CAST(ReadDate AS DATETIME) + ReadTime BETWEEN '2010-09-16 5:00PM' AND '2010-09-21 9:00AM'


You can try this:

SELECT * FROM MYTABLE WHERE DATE BETWEEN '03/10/2014 06:25:00' and '03/12/2010 6:25:00'


use DBName

select *
from TABLE_NAME A
where A.date >= '2018-06-26 21:24' and A.date <= '2018-06-26 21:28';


How about this?

SELECT Value, ReadTime, ReadDate
FROM YOURTABLE
WHERE CAST(ReadDate AS DATETIME) + ReadTime BETWEEN '2010-09-16 17:00:00' AND '2010-09-21 09:00:00'

EDIT: output according to OP's wishes ;-)


Remember that the US date format is different from the UK. Using the UK format, it needs to be, e.g.

--                                  dd/mm/ccyy hh:mm:ss     
dbo.no_time(at.date_stamp) between '22/05/2016 00:00:01' and '22/07/2016 23:59:59' 


DATE(readingstamp) 
BETWEEN '2016-07-21' 
AND '2016-07-31' 
AND TIME(readingstamp)
BETWEEN '08:00:00'
AND '17:59:59'

simply separate the casting of date and time


"SELECT Applicant.applicantId, Applicant.lastName, Applicant.firstName, Applicant.middleName, Applicant.status,Applicant.companyId, Company.name, Applicant.createDate FROM (Applicant INNER JOIN Company ON Applicant.companyId = Company.companyId) WHERE Applicant.createDate between  '" +dateTimePicker1.Text.ToString() + "'and '"+dateTimePicker2.Text.ToString() +"'";

This is what I did.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜