2

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.

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.

1
  • Well I'd start by adding a space after between, and using full yyyy-MM-dd format, i.e. 08 instead of 8 for the month. I realize it's unlikely to make a different, but give that a try to start with. What's the type of the Date field? Commented Oct 11, 2014 at 7:48

2 Answers 2

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Presumably only if Date isn't the right type already... I've asked the OP to clarify.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.