0

Im using vb.net code with Sql Server 2008 R2.
I'm trying to get result by using a SQL query to get all rows that have a value between 2 dates: Here is my the where clause of my statement:

Where (CONVERT(varchar(10), visit_nextVisitDate, 103) between '02/04/2017' AND  '15/05/2017')"

but I always get all rows for the same month (month 4). I tried this:

WHERE (CAST(dbo.Visits.visit_date AS date) BETWEEN '24/04/2017' AND '02/05/2017')

but I got an error cause my date fields are saved in format yyyy/mm/dd

How can I change the SQL date format to dd/mm/yyyy?

1 Answer 1

6

Why would you do date comparisons using strings? That is just wrong, wrong, wrong. (If you do it, use ANSI standard formats, YYYY-MM-DD so the comparisons are correct.)

Just do this using dates:

Where visit_nextVisitDate between '2017-04-02' AND '2017-05-02'

Actually, it is a bad idea to use between with dates. Aaron Bertrand has a very good blog on this subject.

I recommend:

Where visit_nextVisitDate >= '2017-04-02' AND 
      visit_nextVisitDate < '2017-05-03'
Sign up to request clarification or add additional context in comments.

3 Comments

but how if i want use the following format dd/mm/yyyy in my sql??
@EyadShahroury . . . I strongly recommend that you use the ISO standard date formats. If you really insist on other date formats, I would suggest that you explicitly convert the constant values to dates using convert().
OK , I will :) , THANKS

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.