查询以选择两个日期之间的数据

时间:2021-11-21 08:35:56

I have a start_date and end_date. I want to get the list of data in between these two dates. Can anyone help me pointing the mistake in my query.

我有一个start_date和end_date。我想获取这两个日期之间的数据列表。任何人都可以帮我指出我的查询中的错误。

select * from [dbo].[User] 
where Date between'2014-8-01' AND '2014-8-30'

There is no error but also the query doesn't return any records.

没有错误,但查询也不返回任何记录。

2 个解决方案

#1


2  

Proper casting is necessary

适当的铸造是必要的

select * from [dbo].[User] 
    where CAST([Date] as DATE) between '2014-08-01' AND '2014-08-30'

Format for Date Passing as Parameter

日期传递格式作为参数

'yyyy-MM-dd'

#2


1  

Better to use >= and < and use time in your where clause instead of using CAST in where. Better performance.

最好使用> =和 <并在where子句中使用time而不是在where中使用cast。更好的性能。< p>

SELECT * FROM [dbo].[User] 
    WHERE [Date] >= '2014-8-01 00:00:00'
    AND [Date] < '2014-8-31 00:00:00'

#1


2  

Proper casting is necessary

适当的铸造是必要的

select * from [dbo].[User] 
    where CAST([Date] as DATE) between '2014-08-01' AND '2014-08-30'

Format for Date Passing as Parameter

日期传递格式作为参数

'yyyy-MM-dd'

#2


1  

Better to use >= and < and use time in your where clause instead of using CAST in where. Better performance.

最好使用> =和 <并在where子句中使用time而不是在where中使用cast。更好的性能。< p>

SELECT * FROM [dbo].[User] 
    WHERE [Date] >= '2014-8-01 00:00:00'
    AND [Date] < '2014-8-31 00:00:00'