Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Select records between a date range Expand / Collapse
Author
Message
Posted Thursday, January 3, 2008 9:43 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, May 14, 2009 10:11 PM
Points: 475, Visits: 364
This is my query:

SELECT * FROM [MyDateTable]
WHERE CONVERT(VARCHAR,StartDate,101)
BETWEEN '02/01/2007' AND '01/04/2008'

It return absolutely nothing, although there are eleven eligible records within this date range. few in Feb 2007 and others in May 2007.

What could be wrong in this?
Post #438717
Posted Thursday, January 3, 2008 10:01 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:56 PM
Points: 36,772, Visits: 31,227
It's because you're converting the dates to text... try this, instead...

SELECT * FROM [MyDateTable]
WHERE StartDate >= '02/01/2007'
AND StartDate < '01/05/2008'

Notice that the "<" is in relation to the NEXT day after you want... this is to allow all the times on 01/04/2008 to be included without a conversion to a whole date. This method also allows for the use of an index "seek" whereas your method will not.



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #438724
Posted Thursday, January 3, 2008 10:05 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:20 PM
Points: 7,120, Visits: 15,014
Don't convert your dates to string - convert your strings to dates. As of right now you're comparing string values (where '02/01/2007' >'01/28/2008').

you want:
SELECT * FROM [MyDateTable]
WHERE StartDate
BETWEEN cast('02/01/2007' as datetime)
AND cast('01/04/2008' as datetime)

of course - you can also let T-SQL do the implicit conversion for you with:
SELECT * FROM [MyDateTable]
WHERE StartDate
BETWEEN '02/01/2007' --this gets converted to a date
AND '01/04/2008' --this gets converted to a date



----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #438725
Posted Thursday, January 3, 2008 10:48 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, May 14, 2009 10:11 PM
Points: 475, Visits: 364
Perfect, Matt! That's just what I wanted. Figured it out only after i hurriedly posted the query :)
Thanks, Jeff!
Post #438734
Posted Thursday, January 3, 2008 11:26 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:56 PM
Points: 36,772, Visits: 31,227
No... it's not perfect if you want to include the whole day of 01/04/2008...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #438747
Posted Thursday, January 3, 2008 11:44 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, May 14, 2009 10:11 PM
Points: 475, Visits: 364
What's perfect is: 'Don't convert your dates to string - convert your strings to dates'

That's all i needed :) cuz the query I've written uses the comparison operators '>=' and '<='.. which is why I said: 'Thanks, Jeff'!
Post #438756
Posted Friday, January 4, 2008 12:22 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:56 PM
Points: 36,772, Visits: 31,227
You're missing the point... you will not get all of 01/04 using BETWEEN...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #438762
Posted Friday, January 4, 2008 12:28 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, May 14, 2009 10:11 PM
Points: 475, Visits: 364
Ok.. Here's my query:


SELECT * FROM [MyDateTable]
WHERE StartDate >= CONVERT(DATETIME, '02/01/2007')
AND StartDate <= CONVERT(DATETIME, '01/04/2008')

This works fine, Jeff! Which is why I was saying, I used Matt's ''don't convert your date field to string'' and I used your ' operators instead of the BETWEEN'.

Do you think I've gotten the point now? :)
Post #438765
Posted Friday, January 4, 2008 12:32 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:56 PM
Points: 36,772, Visits: 31,227
No...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #438767
Posted Friday, January 4, 2008 12:34 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:56 PM
Points: 36,772, Visits: 31,227
Unless none of your dates have times on them, the original query and the replacement query are bad...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #438768
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse