1

I am in need of your help as I am struggling with a query in SQL. This query is not giving exact result that I want. I need to pull out all the dates between two dates.

I tried two queries:

SELECT a.MemberID,CONVERT(varchar(12), b.BirthDate, 101) AS BirthDate
FROM Members_Hired AS a
    INNER JOIN Members AS b
        ON a.ReferenceID = b.RecordID
WHERE (CONVERT(varchar(12), b.BirthDate, 101) >= '02/01/2013'
    and CONVERT(varchar(12), b.BirthDate, 101) <= '02/27/2013')

and

SELECT a.MemberID,CONVERT(varchar(12), b.BirthDate, 101) AS BirthDate
FROM Members_Hired AS a
    INNER JOIN Members AS b
        ON a.ReferenceID = b.RecordID
WHERE (CONVERT(varchar(12), b.BirthDate, 101) BETWEEN '02/01/2013' and '02/27/2013')

This gives results with the date '02/05/1990', '02/17/1990' also. It considers only month and date, not the year exactly.

4
  • Won't 1990 be the year? Commented Feb 25, 2013 at 23:16
  • If you are comparing dates, you have to reverse it so yyyy comes first, then mm, then dd. If birthdate is a date field, you could potentially convert it using to_char(b.birthdate,'yyyy-mm-dd') - that would be Oracle syntax though which is why I'm posting this as a comment and not an answer Commented Feb 25, 2013 at 23:17
  • What type is b.BirthDate? Is it date, datetime, or something else? Commented Feb 25, 2013 at 23:20
  • 1
    While the answers given are right about using date/time types for comparisons, they all miss one thing - they shouldn't be using BETWEEN (especially on SQL Server). Anything else is the wrong way to think of time... Commented Feb 26, 2013 at 0:09

3 Answers 3

8

You should compare dates not varchar values. Change your where clause as below. Also note that I am using ISO format (yyyymmdd) for string dates before converting to make sure it works in any culture.

b.birthdate between convert(date,'20130201' ) and 
                  convert(date, '20130227')
Sign up to request clarification or add additional context in comments.

1 Comment

I'd give another upvote if I could for using a dateformat that is not culture specific too.
2

I think you are comparing the string instead of date time, so the results have the records '02/25' & '02/17'.

1 Comment

remove all "CONVERT" from your "where" clause.
1

Assuming that your birthdate field is a DateTime type you need to convert you inputs to the same type.

Did you try?

WHERE (b.BirthDate BETWEEN Cast('02/01/2013' As DateTime) and Cast('02/27/2013' As DateTime))

3 Comments

Dangerous. What if I'm in the UK? Now it's looking for January 2 instead of February 1.
The dates where the OP's dates. Am I incorrect that the Cast is culture respective? If the OP was in the UK, the date parameters would be different not the statement (01/02/2013 and 27/02/2013)? Just asking
My point is that encouraging regional formats (even if the OP introduced them) is a bad idea. The reason? The next reader of your answer might not have the same requirements as the OP, and may be looking for 2/1 -> 2/8, so the "obvious" format in your answer might be totally missed. Please read sqlblog.org/2009/10/16/… and sqlblog.org/2011/10/19/…

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.