Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble querying a specific date

Status
Not open for further replies.

Bigced21

Programmer
Feb 5, 2003
76
US
I'm using sql server 2000.
What I'm trying to do is write a select statement to query all records with a date of 6-14-2004, and I'm not getting any results. Could someone please give me some ideas. In access it's easy as hell, but this is throwing me in a loop right now.

the data type for the date is datetime with a default value of (getdate())
 
Hi,

Have you tried using '2004-14-06 00:00:00' (Inc ' marks)?

I've had similar problems in the past ant this is the fix i've used.

Hope this helps.


Leigh Moore
Business Systems Manager
Vets4Pets Veterinary Group
 
The problem is most likely that when you type:
Code:
SELECT  *
  FROM  MyTable
  WHERE DateField = '06-14-2004'
SQL Server interprets the date as a datetime with the time portion set to midnight (00:00:00.000).

To get the full day in the query, you could try:
Code:
SELECT  *
  FROM  MyTable
  WHERE DateField BETWEEN '06-14-2004 00:00:00.000' AND '06-14-2004 23:59:59.999'
or
Code:
SELECT  *
  FROM  MyTable
  WHERE DateField >= '06-14-2004'
    AND DateField < '06-15-2004'
HTH,
John

PS - Someone else will probably have a 'better' way to code this shortly ;-)
 
Ok, John76 that worked!! Thanks!!
and thank you too leighmoore!!!!
 
Code:
SELECT  *
  FROM  MyTable
  WHERE convert(datetime,convert(char(10),DateField,101)) = '06-14-2004'
 
checkai,

That will work, but I don't believe that all of that is neccessary:

Code:
SELECT  *
  FROM  MyTable
  WHERE convert(varchar, DateField, 101) = '06-14-2004'
works fine as well.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top